Schauen Sie sich https://github.com/vvbogdan/BVCropPhoto an
- (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) * scale,
(self.scrollView.contentInset.top + self.scrollView.contentOffset.y) * scale,
self.cropSize.width * scale,
self.cropSize.height * scale);
CGImageRef contextImage = CGImageCreateWithImageInRect ([[self imageWithRotation: self.sourceImage] CGImage], targetFrame);
if (contextImage! = NULL) {
finalImage = [UIImage imageWithCGImage: contextImage
scale: self.sourceImage.scale
Orientierung: UIImageOrientationUp];
CGImageRelease (contextImage);
}}
return finalImage;
}}
- (UIImage *) imageWithRotation: (UIImage *) image {
if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate (transform, M_PI);
brechen;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transform = CGAffineTransformRotate (transform, M_PI_2);
brechen;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, 0, image.size.height);
transform = CGAffineTransformRotate (transform, -M_PI_2);
brechen;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
brechen;
}}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transform = CGAffineTransformScale (transform, -1, 1);
brechen;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, image.size.height, 0);
transform = CGAffineTransformScale (transform, -1, 1);
brechen;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
brechen;
}}
// Jetzt zeichnen wir das zugrunde liegende CGImage in einen neuen Kontext und wenden die Transformation an
// oben berechnet.
CGContextRef ctx = CGBitmapContextCreate (NULL, image.size.width, image.size.height,
CGImageGetBitsPerComponent (image.CGImage), 0,
CGImageGetColorSpace (image.CGImage),
CGImageGetBitmapInfo (image.CGImage));
CGContextConcatCTM (ctx, transform);
switch (image.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr ...
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.height, image.size.width), image.CGImage);
brechen;
Standard:
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.width, image.size.height), image.CGImage);
brechen;
}}
// Und jetzt erstellen wir einfach ein neues UIImage aus dem Zeichnungskontext
CGImageRef cgimg = CGBitmapContextCreateImage (ctx);
UIImage * img = [UIImage imageWithCGImage: cgimg];
CGContextRelease (ctx);
CGImageRelease (cgimg);
return img;
}}