How to crop UIImagePickerController taken picture as it is on display

Adam Bardon picture Adam Bardon · Sep 21, 2013 · Viewed 19.9k times · Source

When I take photo using UIImagePickerController, there is more picture than was visible on screen... How can I take photo/crop it, so that it's exactly the same as I saw on screen?

edit: I'll add code, to specify my problem:

- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls = NO;
imagePickerController.allowsEditing = YES;

if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
    [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
    self.overlayView.frame = imagePickerController.cameraOverlayView.frame;
    imagePickerController.cameraOverlayView = self.overlayView;
    //self.overlayView = nil;
    self.overlayImage.image = self.imageView.image;
    self.overlayImage.alpha = 0.5;

    UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake(60, 80, 200, 30)];
    [slider setMaximumValue:1.0];
    [slider setMinimumValue:0.0];
    [slider setValue:0.5];
    [slider addTarget:self action:@selector(sliderChanged:)  forControlEvents:UIControlEventValueChanged];

    if (self.working == TRUE)
    {
        [imagePickerController.cameraOverlayView addSubview:self.overlayImage];
        [imagePickerController.cameraOverlayView addSubview:slider];
    }
}

self.imagePickerController = imagePickerController;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
}

didFinishPickingMediaWithInfo:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];

//this works, image is displayed
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//this doesn't work, image is nil
UIImage *image = [info objecyForKey:UIImagePickerControllerEditedImage];    

[self.imageView setImage:image];
self.working = TRUE;

self.imagePickerController = nil;
//[self dismissViewControllerAnimated:YES completion:NULL];
}

Can I use UIImagePickerControllerEditedImage, when imagePickerController.showsCameraControls = NO;?

Answer

Rok Jarc picture Rok Jarc · Sep 21, 2013

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