Mirar 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) * 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
orientación: UIImageOrientationUp];
CGImageRelease (contextImage);
}
return finalImage;
}
- (UIImage *) imageWithRotation: (UIImage *) image {
if (image.imageOrientation == UIImageOrientationUp) devuelve la imagen;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
caso UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate (transform, M_PI);
descanso;
caso UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transformar = CGAffineTransformRotate (transformar, M_PI_2);
descanso;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, 0, image.size.height);
transformar = CGAffineTransformRotate (transformar, -M_PI_2);
descanso;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
descanso;
}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transformar = CGAffineTransformScale (transformar, -1, 1);
descanso;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, image.size.height, 0);
transformar = CGAffineTransformScale (transformar, -1, 1);
descanso;
case UIImageOrientationUp:
caso UIImageOrientationDown:
caso UIImageOrientationLeft:
case UIImageOrientationRight:
descanso;
}
// Ahora dibujamos la imagen CG subyacente en un nuevo contexto, aplicando la transformación
// calculado arriba.
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) {
caso UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr ...
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.height, image.size.width), image.CGImage);
descanso;
defecto:
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.width, image.size.height), image.CGImage);
descanso;
}
// Y ahora solo creamos un nuevo UIImage del contexto de dibujo
CGImageRef cgimg = CGBitmapContextCreateImage (ctx);
UIImage * img = [UIImage imageWithCGImage: cgimg];
CGContextRelease (ctx);
CGImageRelease (cgimg);
volver img;
}