Spójrz na https://github.com/vvbogdan/BVCropPhoto
- (UIImage *) croppedImage {
CGFloat scale = self.sourceImage.size.width / self.scrollView.contentSize.width;
UIImage * finalImage = zero;
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
orientacja: UIImageOrientationUp];
CGImageRelease (contextImage);
}
zwróć końcowy obraz;
}
- (UIImage *) imageWithRotation: (UIImage *) image {
if (image.imageOrientation == UIImageOrientationUp) zwraca obraz;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate (transformacja, M_PI);
przerwa;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transform = CGAffineTransformRotate (transformacja, M_PI_2);
przerwa;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, 0, image.size.height);
transform = CGAffineTransformRotate (transformacja -M_PI_2);
przerwa;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
przerwa;
}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate (transform, image.size.width, 0);
transform = CGAffineTransformScale (transformata, -1, 1);
przerwa;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate (transform, image.size.height, 0);
transform = CGAffineTransformScale (transformata, -1, 1);
przerwa;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
przerwa;
}
// Teraz rysujemy bazowy CGImage w nowym kontekście, stosując transformację
// obliczone powyżej.
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);
przerwa;
domyślna:
CGContextDrawImage (ctx, CGRectMake (0, 0, image.size.width, image.size.height), image.CGImage);
przerwa;
}
// A teraz po prostu tworzymy nowego UIImage na podstawie rysunku
CGImageRef cgimg = CGBitmapContextCreateImage (ctx);
UIImage * img = [UIImage imageWithCGImage: cgimg];
CGContextRelease (ctx);
CGImageRelease (cgimg);
zwróć img;
}