Core Graphics

//字體

//  MyView.matom

//  Core Graphics次日spa

//3d

//  Created by dc008 on 15/12/8.orm

//  Copyright © 2015 CXY. All rights reserved.圖片

//ip


#import "MyView.h"it


@implementation MyViewio



- (void)drawRect:(CGRect)rect {table

    CGContextRef  context = UIGraphicsGetCurrentContext();

//    [self drawText:context];

//    [self drawImage:context];

    [self drawImageAtImageContext:context];

}

#pragma mark 繪製文字

- (void)drawText : (CGContextRef) context{

   NSString *str = @"今天咱們舉辦了一年一度的繪圖大賽,同窗們踊躍參與。";

   //定義文字顯示區域

    CGRect rect = CGRectMake(20, 50, 335, 300);

   //創建一個段落樣式

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];

    style.alignment = NSTextAlignmentCenter;//居中

    //繪製文字,設置屬性(字體大小,顏色)

    [str drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName:style}];

 }


#pragma mark 繪製圖片

- (void) drawImage : (CGContextRef) context

{

    UIImage *image = [UIImage imageNamed:@"1"];

    //1.從某一點開始繪製

//    [image drawAtPoint:CGPointMake(0, 20)];

    //2.繪製到指定的矩形中,注意:若是大小不合適會進行拉伸

    [image drawInRect:CGRectMake(0, 100, 375, 667)];

    

    UIImage *imageTwo = [UIImage imageNamed:@"2"];

    [image drawInRect:CGRectMake(0, 300, 200, 200)];

    //3.平鋪繪製

//    [image drawAsPatternInRect:CGRectMake(0, 0, 375, 667)];

}

#pragma mark 水印(圖片上添加圖片)

- (void)drawImageAtImageContext : (CGContextRef) context{

    //開始圖片上下文(設置畫布大小)

    UIGraphicsBeginImageContext(CGSizeMake(375, 667));

    //建立圖片

    UIImage *image = [UIImage imageNamed:@"2"];

    //裁剪圖片->橢圓

    context=UIGraphicsGetCurrentContext();//從新獲取上下文

    CGContextAddEllipseInRect(context, CGRectMake(0, 0, 375, 375));

    CGContextClip(context);//裁剪

    [image drawInRect:CGRectMake(0, 0, 375, 375)];

    //添加水印

    NSString *str = @"Photo by cxy";

    [str drawInRect:CGRectMake(375/2.0-50, 667/2-10, 100, 30) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15],NSForegroundColorAttributeName:[UIColor whiteColor]}];

    //返回繪製的新圖像

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    //關閉對應的上下文

    UIGraphicsEndImageContext();

    [newImage drawInRect:CGRectMake(0, 0, 375, 667)];

    //保存圖片

    NSData *data = UIImagePNGRepresentation(newImage) ;

    [data writeToFile:@"/Users/dc008/Desktop/photo.png" atomically:YES];

    

    UIGraphicsBeginImageContext(CGSizeMake(375, 667));

    UIImage *imageTwo = [UIImage imageNamed:@"1"];

    context = UIGraphicsGetCurrentContext();

    CGContextAddEllipseInRect(context, CGRectMake(0, 0, 375, 375));

    CGContextClip(context);

    [imageTwo drawInRect:CGRectMake(0, 0, 375, 667)];

    NSString *strTwo = @"Photo by cxy";

    [strTwo drawInRect:CGRectMake(375/2.0-50, 667/2-10, 100, 30) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15],NSForegroundColorAttributeName:[UIColor whiteColor]}];

    UIImage *newImageTwo = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    [newImageTwo drawInRect:CGRectMake(0, 0, 375, 667)];

    NSData *dataTwo = UIImagePNGRepresentation(newImageTwo);

    [dataTwo writeToFile:@"/Users/dc008/Desktop/photoTwo.png" atomically:YES];

}


@end


//

//  ViewController.m

//  Core Graphics次日

//

//  Created by dc008 on 15/12/8.

//  Copyright © 2015 CXY. All rights reserved.

//


#import "ViewController.h"

#import "MyView.h"

@interface ViewController ()




@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

     

    MyView *myView = [[MyView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    myView.backgroundColor = [UIColor colorWithRed:0.11 green:0.7 blue:0.6 alpha:0.3];

    [self.view addSubview:myView];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相關文章
相關標籤/搜索