1.加载要裁剪的图片atom
2.开启一个和图片同样大小的图像上下文(bitmap 图片类型的上下文)spa
UIGraphicsBeginImageContextWithOptions(ctxSize, NO, 0.0);orm
3.获取刚刚开启的图像上下文图片
CGContextRef ctx = UIGraphicsGetCurrentContext();ip
4.建立一个圆形路径get
UIBezierPath * path1 = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];string
5.把路径添加到图像上下文中it
CGContextAddPath(ctx, path1.CGPath);io
6.执行裁剪ast
CGContextClip(ctx);
6.1把图片绘制到图像上下文中
[image drawAtPoint:CGPointMake(margin, margin)];
7.从图像上下文中获取图片
UIImage * getImage = UIGraphicsGetImageFromCurrentImageContext();
8.关闭图形上下文
UIGraphicsEndImageContext();
9.显示图片
self.imageView.image = getImage;
10.保存相册
UIImageWriteToSavedPhotosAlbum(getImage, self, @selector(image:didFinishSavingWithError:contextInfo:), @"hello word");
//写入相册必定实现该方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
// NSLog(@"保存成功 %@",contextInfo);
}
11.保存到沙盒中
NSString * docuStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//拼接文件名
NSString * fileName = [docuStr stringByAppendingPathComponent:@"001.png"];
//把图片转化为NSData类型
NSData * imageData = UIImagePNGRepresentation(getImage);
[imageData writeToFile:fileName atomically:YES];