Core Graphics

本文介绍了一个iOS应用中使用Core Graphics进行复杂绘图的方法,包括文本绘制、图片展示及如何实现带有水印效果的图片。通过具体的代码实例展示了如何在iOS应用中实现这些功能。

//

//  MyView.m

//  Core Graphics第二天

//

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

//  Copyright © 2015 CXY. All rights reserved.

//


#import "MyView.h"


@implementation MyView



- (void)drawRect:(CGRect)rect {

    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


转载于:https://my.oschina.net/u/2499773/blog/541029

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值