Veja https://github.com/vvbogdan/BVCropPhoto
- (UIImage *) croppedImage {
Escala CGFloat = self.sourceImage.size.width / self.scrollView.contentSize.width;
UIImage * finalImage = nulo;
CGRect targetFrame = CGRectMake ((self.scrollView.contentInset.left + self.scrollView.contentOffset.x) * escala,
(self.scrollView.contentInset.top + self.scrollView.contentOffset.y) * escala,
escala self.cropSize.width *,
self.cropSize.height * escala);
CGImageRef contextImage = CGImageCreateWithImageInRect ([[auto imageWithRotation: self.sourceImage] CGImage], targetFrame);
if (contextImage! = NULL) {
finalImage = [UIImage imageWithCGImage: contextImage
scale: self.sourceImage.scale
orientação: UIImageOrientationUp];
CGImageRelease (contextImage);
}
return finalImage;
}
- (UIImage *) imageWithRotation: (UIImage *) imagem {
if (image.imageOrientation == UIImageOrientationUp) retorna imagem;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, image.size.height);
transformar = CGAffineTransformRotate (transformar, M_PI);
pausa;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transformar = CGAffineTransformRotate (transformar, M_PI_2);
pausa;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, 0, image.size.height);
transformar = CGAffineTransformRotate (transformar, -M_PI_2);
pausa;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
pausa;
}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transformar = CGAffineTransformScale (transformar, -1, 1);
pausa;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, image.size.height, 0);
transformar = CGAffineTransformScale (transformar, -1, 1);
pausa;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
pausa;
}
// Agora, desenhamos o CGImage subjacente em um novo contexto, aplicando a transformação
// calculado acima.
CGContextRef ctx = CGBitmapContextCreate (NULL, image.size.width, image.size.height,
CGImageGetBitsPerComponent (image.CGImage), 0,
CGImageGetColorSpace (image.CGImage),
CGImageGetBitmapInfo (image.CGImage));
CGContextConcatCTM (ctx, transformação);
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);
pausa;
padrão:
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.width, image.size.height), image.CGImage);
pausa;
}
// E agora criamos uma nova UIImage a partir do contexto do desenho
CGImageRef cgimg = CGBitmapContextCreateImage (ctx);
UIImage * img = [UIImage imageWithCGImage: cgimg];
CGContextRelease (ctx);
CGImageRelease (cgimg);
return img;
}