Lihatlah https://github.com/vvbogdan/BVCropPhoto
- (UIImage *) croppedImage {
Skala CGFloat = self.sourceImage.size.width / self.scrollView.contentSize.width;
UIImage * finalImage = nil;
CGRect targetFrame = CGRectMake ((self.scrollView.contentInset.left + self.scrollView.contentOffset.x) * skala,
(self.scrollView.contentInset.top + self.scrollView.contentOffset.y) * skala,
skala self.cropSize.width *,
skala self.cropSize.height *);
CGImageRef contextImage = CGImageCreateWithImageInRect ([[self imageWithRotation: self.sourceImage] CGImage], targetFrame);
if (contextImage! = NULL) {
finalImage = [UIImage imageWithCGImage: contextImage
skala: self.sourceImage.scale
orientasi: UIImageOrientationUp];
CGImageRelease (contextImage);
}
mengembalikan finalImage;
}
- (UIImage *) imageWithRotation: (UIImage *) image {
if (image.imageOrientation == UIImageOrientationUp) mengembalikan gambar;
Transformasi CGAffineTransform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate (transform, M_PI);
istirahat;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transform = CGAffineTransformRotate (transform, M_PI_2);
istirahat;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, 0, image.size.height);
transform = CGAffineTransformRotate (transform, -M_PI_2);
istirahat;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
istirahat;
}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transform = CGAffineTransformScale (transform, -1, 1);
istirahat;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, image.size.height, 0);
transform = CGAffineTransformScale (transform, -1, 1);
istirahat;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
istirahat;
}
// Sekarang kita menggambar CGImage yang mendasarinya ke dalam konteks baru, menerapkan transformasi
// dihitung di atas.
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);
istirahat;
default:
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.width, image.size.height), image.CGImage);
istirahat;
}
// Dan sekarang kita baru saja membuat UIImage baru dari konteks gambar
CGImageRef cgimg = CGBitmapContextCreateImage (ctx);
UIImage * img = [UIImage imageWithCGImage: cgimg];
CGContextRelease (ctx);
CGImageRelease (cgimg);
kembali img;
}