Regardez https://github.com/vvbogdan/BVCropPhoto
- (UIImage *) croppedImage {
Échelle CGFloat = self.sourceImage.size.width / self.scrollView.contentSize.width;
UIImage * finalImage = nil;
CGRect targetFrame = CGRectMake ((self.scrollView.contentInset.left + self.scrollView.contentOffset.x) * échelle,
(self.scrollView.contentInset.top + self.scrollView.contentOffset.y) * échelle,
échelle self.cropSize.width *,
échelle 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);
}
return finalImage;
}
- (UIImage *) imageWithRotation: (UIImage *) image {
if (image.imageOrientation == UIImageOrientationUp) renvoie l'image;
CGAffineTransform transform = CGAffineTransformIdentity;
commutateur (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate (transform, M_PI);
Pause;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transform = CGAffineTransformRotate (transform, M_PI_2);
Pause;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, 0, image.size.height);
transform = CGAffineTransformRotate (transform, -M_PI_2);
Pause;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
Pause;
}
commutateur (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transform = CGAffineTransformScale (transform, -1, 1);
Pause;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, image.size.height, 0);
transform = CGAffineTransformScale (transform, -1, 1);
Pause;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
Pause;
}
// Maintenant, nous dessinons la CGImage sous-jacente dans un nouveau contexte, en appliquant la transformation
// calculé ci-dessus.
CGContextRef ctx = CGBitmapContextCreate (NULL, image.size.width, image.size.height,
CGImageGetBitsPerComponent (image.CGImage), 0,
CGImageGetColorSpace (image.CGImage),
CGImageGetBitmapInfo (image.CGImage));
CGContextConcatCTM (ctx, transform);
commutateur (image.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr ...
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.height, image.size.width), image.CGImage);
Pause;
défaut:
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.width, image.size.height), image.CGImage);
Pause;
}
// Et maintenant, nous créons juste un nouveau UIImage à partir du contexte de dessin
CGImageRef cgimg = CGBitmapContextCreateImage (ctx);
UIImage * img = [UIImage imageWithCGImage: cgimg];
CGContextRelease (ctx);
CGImageRelease (cgimg);
return img;
}