How to crop UIImagePickerController taken picture as it is on display How to crop UIImagePickerController taken picture as it is on display objective-c objective-c

How to crop UIImagePickerController taken picture as it is on display


You can get the original or cropped image depending on the key for info dictionary.

The pickedImageEdited would be the cropped image you are looking for and pickedImageOriginal would be the full original image.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{    UIImage *pickedImageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];    UIImage *pickedImageEdited = [info objectForKey:UIImagePickerControllerEditedImage];    //do your stuff    [self dismissViewControllerAnimated:YES completion:nil];}


Just posting this as an answer, but I am not sure if the poster asks for the edited image.

When you take a picture with the UIImagePickerController, the picker shows only part of the image that is actually captured when taking the photo, like shown in the picture below where the black lines are the screen of the iPhone.

enter image description here

The image you get from the camera is the full size image, but on screen is only the center of the image, with either the width or the height of the image maxed out to the screen size.

All you need to do is get the center from the image like shown above and you have the exact image you had on your screen.


After Wim's answer, I was able to come up with solution like this:

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];UIGraphicsBeginImageContext(CGSizeMake(720, 960));[image drawInRect: CGRectMake(0, 0, 720, 960)];UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();CGRect cropRect = CGRectMake(40, 0, 640, 960);CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);[self.imageView setImage:[UIImage imageWithCGImage:imageRef]];

As you can see, this is made for 3,5" iPhone screen, and it seems to work, but code is device-dependent, or is there better solution?