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);
if (contextImage! = NULL) {
finalImage = [UIImage imageWithCGImage : contextImage
scale : self.sourceImage.scale
orientation : UIImageOrientationUp];
CGImageRelease (contextImage);
}
finalImage를 반환;
}
-(UIImage *) imageWithRotation : (UIImage *) 이미지 {
if (image.imageOrientation == UIImageOrientationUp) 이미지를 반환합니다;
CGAffineTransform 변환 = CGAffineTransformIdentity;
스위치 (image.imageOrientation) {
case UIImageOrientationDown :
case UIImageOrientationDownMirrored :
변환 = CGAffineTransformTranslate (변환, 이미지 크기, 너비, 이미지 크기);
변환 = CGAffineTransformRotate (변환, M_PI);
단절;
case UIImageOrientationLeft :
case UIImageOrientationLeftMirrored :
변환 = CGAffineTransformTranslate (변환, image.size.width, 0);
변환 = CGAffineTransformRotate (변환, M_PI_2);
단절;
case UIImageOrientationRight :
case UIImageOrientationRightMirrored :
변환 = CGAffineTransformTranslate (변환, 0, image.size.height);
변환 = CGAffineTransformRotate (변환, -M_PI_2);
단절;
case UIImageOrientationUp :
case UIImageOrientationUp 미러 :
단절;
}
스위치 (image.imageOrientation) {
case UIImageOrientationUp 미러 :
case UIImageOrientationDownMirrored :
변환 = CGAffineTransformTranslate (변환, image.size.width, 0);
변환 = CGAffineTransformScale (변환, -1, 1);
단절;
case UIImageOrientationLeftMirrored :
case UIImageOrientationRightMirrored :
변환 = CGAffineTransformTranslate (변환, 이미지 크기. 높이, 0);
변환 = CGAffineTransformScale (변환, -1, 1);
단절;
case UIImageOrientationUp :
case UIImageOrientationDown :
case UIImageOrientationLeft :
case 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) {
case UIImageOrientationLeft :
case UIImageOrientationLeftMirrored :
case UIImageOrientationRight :
case UIImageOrientationRightMirrored :
// Grr ...
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);
img를 반환;
}