看看https://github.com/vvbogdan/BVCropPhoto
- (UIImage *)croppedImage {
CGFloat scale = self.sourceImage.size.width / self.scrollView.contentSize.width;
UIImage *finalImage = nil;
CGRect targetFrame = CGRectMake((self.scrollView.contentInset.left + self.scrollView.contentOffset.x) * 比例,
(self.scrollView.contentInset.top + self.scrollView.contentOffset.y) * 比例,
self.cropSize.width * 比例,
self.cropSize.height * 比例);
CGImageRef contextImage = CGImageCreateWithImageInRect([[self imageWithRotation:self.sourceImage] CGImage], targetFrame);
如果(上下文图像!= NULL){
finalImage = [UIImage imageWithCGImage:contextImage
规模:self.sourceImage.scale
方向:UIImageOrientationUp];
CGImageRelease(contextImage);
}
返回最终图像;
}
- (UIImage *)imageWithRotation:(UIImage *)image {
if (image.imageOrientation == UIImageOrientationUp) 返回图片;
CGAffineTransform 变换 = CGAffineTransformIdentity;
开关(image.imageOrientation){
案例 UIImageOrientationDown:
案例 UIImageOrientationDownMirrored:
变换 = CGAffineTransformTranslate(变换,image.size.width,image.size.height);
变换 = CGAffineTransformRotate(变换,M_PI);
休息;
案例 UIImageOrientationLeft:
案例 UIImageOrientationLeftMirrored:
变换 = CGAffineTransformTranslate(变换, image.size.width, 0);
变换 = CGAffineTransformRotate(变换,M_PI_2);
休息;
案例 UIImageOrientationRight:
案例 UIImageOrientationRightMirrored:
变换 = CGAffineTransformTranslate(变换, 0, image.size.height);
变换 = CGAffineTransformRotate(变换,-M_PI_2);
休息;
案例 UIImageOrientationUp:
案例 UIImageOrientationUpMirrored:
休息;
}
开关(image.imageOrientation){
案例 UIImageOrientationUpMirrored:
案例 UIImageOrientationDownMirrored:
变换 = CGAffineTransformTranslate(变换, image.size.width, 0);
变换 = CGAffineTransformScale(变换, -1, 1);
休息;
案例 UIImageOrientationLeftMirrored:
案例 UIImageOrientationRightMirrored:
变换 = CGAffineTransformTranslate(变换, image.size.height, 0);
变换 = CGAffineTransformScale(变换, -1, 1);
休息;
案例 UIImageOrientationUp:
案例 UIImageOrientationDown:
案例 UIImageOrientationLeft:
案例 UIImageOrientationRight:
休息;
}
// 现在我们将底层的 CGImage 绘制到一个新的上下文中,应用变换
// 上面计算。
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
CGImageGetBitsPerComponent(image.CGImage), 0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, 变换);
开关(image.imageOrientation){
案例 UIImageOrientationLeft:
案例 UIImageOrientationLeftMirrored:
案例 UIImageOrientationRight:
案例 UIImageOrientationRightMirrored:
// 嘎嘎...
CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.height, image.size.width), image.CGImage);
休息;
默认:
CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
休息;
}
// 现在我们只是从绘图上下文中创建一个新的 UIImage
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
返回图片;
}