Cropping UIImagePickerControllerOriginalImage using UIImagePickerControllerCropRect returns the wrong image

I am trying to edit a captured image and save it in the gallery. I did

UIImagePickerController *picker=[[UIImagePickerController alloc] init]; picker.allowsEditting=YES; 

I want to save the image in an editable square part and save it in the gallery. I know that I can use [info objectForKey:@"UIImagePickerControllerEditedImage"] to save the modified image. But this always returns me a 320x320 size image (iPad Mini), and a poor quality image. Therefore, I planned to crop the original image [info objectForKey:@"UIImagePickerControllerOriginalImage"] with the following code:

 CGRect rect = [[info objectForKey:@"UIImagePickerControllerCropRect"]CGRectValue]; UIImage *originalImage=[info objectForKey:@"UIImagePickerControllerOriginalImage"]; CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], rect); UIImage *result = [UIImage imageWithCGImage:imageRef scale:originalImage.scale orientation:originalImage.imageOrientation]; CGImageRelease(imageRef); 

Then I saved both the result and the edited image ( [info objectForKey:@"UIImagePickerControllerEditedImage"] ). When comparing both images, they match. I attached edited and cropped images. My ultimate goal is to crop the original image to an image in an editable square part and save it in a gallery with good image quality. Can someone please tell me what exactly is going wrong and help me fix this problem?

Thanks in advance. cropped Image

Edited image

+7
ios ipad uiimagepickercontroller
source share
1 answer

I found out the reason why pruning went wrong. The image that is returned by UIImagePickerControllerOriginalImage rotates to -90 degrees. Thus, cropping on the rotated image returned the wrong cropped image to me. So I rotated the image 90 degrees and then cropped it. Finally, I got the expected cropped image with good quality. The following code solved my problem.

  UIImage *originalImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; CGRect rect=[[info objectForKey:@"UIImagePickerControllerCropRect"]CGRectValue]; UIImage *rotatedOriginalImage=[originalImage imageRotatedByDegrees:90.0]; CGImageRef imageRef = CGImageCreateWithImageInRect([rotatedOriginalImage CGImage], rect) ; UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 

To rotate the image:

 - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees{ // calculate the size of the rotated view containing box for our drawing space UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.height, self.size.width)]; CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; // Create the bitmap context UIGraphicsBeginImageContext(rotatedSize); CGContextRef bitmap = UIGraphicsGetCurrentContext(); // Move the origin to the middle of the image so we will rotate and scale around the center. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); // // Rotate the image context CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); // Now, draw the rotated/scaled image into the context CGContextScaleCTM(bitmap, 1.0, -1.0); CGContextDrawImage(bitmap, CGRectMake(-self.size.height / 2, -self.size.width / 2, self.size.height, self.size.width), [self CGImage]); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 
+2
source share

All Articles