8

I am trying to edit a captured Image and save it to gallery. I have made

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

I want to save the image in the editable square portion and save it to gallery. I know i can make use of [info objectForKey:@"UIImagePickerControllerEditedImage"] to save the editted image. But this always returns me an image of dimension 320x320(iPad Mini) and the image is of poor quality. So I planned to crop the original image [info objectForKey:@"UIImagePickerControllerOriginalImage"] by using 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 image and the Edited Image ([info objectForKey:@"UIImagePickerControllerEditedImage"]). When compared both the images, they dint match. I have attached the edited and cropped Images. My ultimate aim is to crop the original image to the image in the editable square portion and save it to gallery with good image quality. Can anyone please tell me on what exactly goes wrong here and help me fix this issue?

Thanks in advance.cropped Image

Edited Image

CrazyDeveloper
  • 995
  • 1
  • 13
  • 24
  • 9
    I've had a similar issue and just wrote a radar and disabled editing. Then I cleaned my keyboard :p – fzwo Aug 26 '13 at 13:06
  • i would appreciate if u can be more specific on the solution. I am badly in need of it. – CrazyDeveloper Aug 27 '13 at 01:14
  • 1
    As I said, I've given up, filed a bug report on https://bugreport.apple.com (internally, the Apple bug tracker is called "Radar"), and moved on without it. If I really, really needed the functionality, I'd look for open source components that duplicate it, or write it myself. I have to admit, though, that it is kinda strange you're not getting *any* answers. Have you searched on the site to see if others had the same problem? – fzwo Aug 27 '13 at 08:23

2 Answers2

2

I found out the reason why cropping went wrong here. The image which is returned by UIImagePickerControllerOriginalImage is rotated to -90 degrees. So cropping on the rotated image returned me an incorrect cropped image. So I rotated the image to 90 degrees and then cropped it. Finally i was got the expected cropped image with good quality. The below code solved my Issue.

 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];

code to rotate Image:

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees{
// calculate the size of the rotated view's 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;
}
CrazyDeveloper
  • 995
  • 1
  • 13
  • 24
0

The bug has something to do with the Status Bar. I found by hiding it in the UIImagePickerController the image gets cropped correctly.

Swift solution here: https://stackoverflow.com/a/41456379

ian
  • 11
  • 1