怎么切图
写上传头像的时候用到的需求,有时候图片需要进行调整,如何根据用户的调整进行切图,使用的是Core Graphics的画布.
@interface YZFHeadImageAdjustViewController ()
@property (nonatomic, strong) UIImage *originalImage;
@end
- (UIImage *)clipEditedImage {
//最终切图rect
CGRect subImageRect = CGRectMake(x, y, width, height);
//通过Core Graphic切图
CGImageRef imageRef = _originalImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, subImageRect);
//获取subImage的大小
CGSize size;
size.width = subImageRect.size.width;
size.height = subImageRect.size.height;
//展开一个和Size大小一样的画布
UIGraphicsBeginImageContext(size);
//获得当前画布
CGContextRef context = UIGraphicsGetCurrentContext();
//在新画布上根据当前画布,画出新画布大小的,subImageRef的内容
CGContextDrawImage(context, subImageRect, subImageRef);
//通过新画布创建新的UIImage
UIImage* subImage = [UIImage imageWithCGImage:subImageRef];
//关闭新画布
UIGraphicsEndImageContext();
//返回小图
return subImage;
}