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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With