标签:
// // ViewController.m // Quartz2D图片处理 // // Created by dllo on 16/3/30. // Copyright © 2016年 HaiTeng. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor cyanColor]; UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; imgV.center = self.view.center; [self.view addSubview:imgV]; // imgV.image = [self addWaterMark:[UIImage imageNamed:@"手艺人.jpg"]]; imgV.image = [self imageCutter:[UIImage imageNamed:@"手艺人.jpg"]]; imgV.image = [self screenShotCut]; self.view.backgroundColor = [UIColor whiteColor]; } #pragma mark - 加水印 - (UIImage *)addWaterMark:(UIImage *)sourceImage { //上下文 位图上下文 //与layer层的上下文不同的是 layer层上下文是获取到的 ,位图上下文需要我们手动创建 //位图上下文与layer层无关 所以不用再DrawRect方法里面实现 //1.上下文 创建位图上下文 /* <#CGSize size#> 上下文大小 <#BOOL opaque#> 是否透明 <#CGFloat scale#> 缩放比例 */ UIGraphicsBeginImageContextWithOptions(sourceImage.size, NO, 0); //2.绘制内容 //绘制源图片到上下文 [sourceImage drawAtPoint:CGPointZero]; //绘制文字在上下文 NSString *str = @"手艺人"; /* 第一个参数: 相对于源图片自己大小 给point值 第二个参数: 给文字设置富文本 */ //设置文字样式 NSMutableDictionary *dic = [NSMutableDictionary dictionary]; dic[NSForegroundColorAttributeName] = [UIColor blackColor]; dic[NSFontAttributeName] = [UIFont boldSystemFontOfSize:50.f]; [str drawAtPoint:CGPointMake(sourceImage.size.width - 200, sourceImage.size.height - 200) withAttributes:dic]; //3.从当前位图上下文 生成带有水印的图片 UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); //4.关闭上下文(放在内存泄露) UIGraphicsEndImageContext(); return img; } #pragma mark - 图片截圆 - (UIImage *)imageCutter:(UIImage *)SourceImg{ //1.开启上下文 UIGraphicsBeginImageContextWithOptions(SourceImg.size, NO, 0); //2.获取裁剪区域 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, SourceImg.size.width, SourceImg.size.height)]; //将路径设置为裁剪区域 [path addClip]; //3.绘制图片 [SourceImg drawAtPoint:CGPointZero]; //4.生成图片 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); //关闭上下文 UIGraphicsEndImageContext(); return image; } #pragma mark 截屏 - (UIImage *)screenShotCut { //开启上下文 UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, NO, 0); //获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //将self.view的控件 渲染到上下文 [self.view.layer renderInContext:ctx]; //生成图片 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); //关闭 UIGraphicsEndImageContext(); return image; } @end
标签:
原文地址:http://www.cnblogs.com/HaiTeng/p/5338223.html