IOS第17天(2,Quartz2D图片剪裁变圆行图,和截屏图片)
2015-09-01 17:19
441 查看
****
*********************工具类
#import "HMViewController.h" #import "UIImage+Tool.h" @interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // _imageView.image = [UIImage imageWithName:@"阿狸头像" border:10 borderColor:[UIColor redColor]]; [self clipCircle]; } - (void)clipCircle { // 圆环的宽度 CGFloat borderW = 5; // 加载旧的图片 UIImage *oldImage = [UIImage imageNamed:@"阿狸头像"]; // 新的图片尺寸 CGFloat imageW = oldImage.size.width + 2 * borderW; CGFloat imageH = oldImage.size.height + 2 * borderW; // 设置新的图片尺寸 CGFloat circirW = imageW > imageH ? imageH : imageW; // 开启上下文 UIGraphicsBeginImageContextWithOptions(CGSizeMake(circirW, circirW), NO, 0.0); // 画大圆 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, circirW, circirW)]; // 获取当前上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 添加到上下文 CGContextAddPath(ctx, path.CGPath); [[UIColor whiteColor] set]; //边框颜色 // 渲染 CGContextFillPath(ctx); CGRect clipR = CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height); // 画圆:正切于旧图片的圆 UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:clipR]; // 设置裁剪区域 [clipPath addClip]; // 画图片 [oldImage drawAtPoint:CGPointMake(borderW, borderW)]; // 获取新的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文 UIGraphicsEndImageContext(); _imageView.image = newImage; } - (void)clip { // 1.加载旧的图片 UIImage *oldImage = [UIImage imageNamed:@"阿狸头像"]; // 2.开启上下文 UIGraphicsBeginImageContextWithOptions(oldImage.size, NO, 0.0); // 3.画圆:正切于上下文 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, oldImage.size.width, oldImage.size.height)]; // 4.设为裁剪区域 [path addClip]; // 5.画图片 [oldImage drawAtPoint:CGPointZero]; // 6.生成一个新的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 7.关闭上下文 UIGraphicsEndImageContext(); } @end
*********************工具类
#import "UIImage+Tool.h" @implementation UIImage (Tool) + (instancetype)imageWithCaptureView:(UIView *)view { // 开启上下文 UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0); // 获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 渲染控制器view的图层到上下文 // 图层只能用渲染不能用draw [view.layer renderInContext:ctx]; // 获取截屏图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文 UIGraphicsEndImageContext(); return newImage; } + (instancetype)imageWithName:(NSString *)name border:(CGFloat)border borderColor:(UIColor *)color { // 圆环的宽度 CGFloat borderW = border; // 加载旧的图片 UIImage *oldImage = [UIImage imageNamed:name]; // 新的图片尺寸 CGFloat imageW = oldImage.size.width + 2 * borderW; CGFloat imageH = oldImage.size.height + 2 * borderW; // 设置新的图片尺寸 CGFloat circirW = imageW > imageH ? imageH : imageW; // 开启上下文 UIGraphicsBeginImageContextWithOptions(CGSizeMake(circirW, circirW), NO, 0.0); // 画大圆 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, circirW, circirW)]; // 获取当前上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 添加到上下文 CGContextAddPath(ctx, path.CGPath); // 设置颜色 [color set]; // 渲染 CGContextFillPath(ctx); CGRect clipR = CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height); // 画圆:正切于旧图片的圆 UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:clipR]; // 设置裁剪区域 [clipPath addClip]; // 画图片 [oldImage drawAtPoint:CGPointMake(borderW, borderW)]; // 获取新的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文 UIGraphicsEndImageContext(); return newImage; } @end
相关文章推荐
- 23.iOS学习笔记(十四)——发短信
- 24. iOS中文网址路径转换URLEncode
- 22.IOS开发中(null)与<null>的处理
- Ios 多国语言的使用(已汉语和英语为例)
- IOS第17天(1,Quartz2D图片水印)
- ios原生二维码扫描
- iOS CGAffineTransform详解
- 16.IOS 新消息通知提示-声音、震动
- IOS第16天(5,Quartz2D雪花)
- IOS第16天(4,Quartz2D柱状图)
- IOS第16天(3,Quartz2D饼图)
- IOS第16天(2,Quartz2D下载进度条)
- 14.IOS中系统唯一标识ID
- iOS KVC,KVO
- 4.极光推送解释的iOS本地通知
- 5.iOS 7 Background Remote Notification
- 2.iOS苹果推送-一些示例代码和注意的问题
- 1.iOS中的系统通知 自定义推送声音
- IOS开发-视频,音频,录音简单总结
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制