Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerController + Cropping: what does allowEditing and what does it not?

After reading the documentation and sample code from apple regarding the UIImagePickerController and after the read of many tutorials and even Questions here at Stackoverflow (or here), i came to the conclusion, that the builtin UIImagePicker is able to present a "crop window" to the User and let the user crop and move the right part of the Image he wants to use.

But checking out these examples on iOS 7.1, there appears no cropping window. There appears nothing what let the user crop out a part of the taken image. All tests occured on real hardware (iPhone 5S), no iPhone simulator was used here because of missing camera ;)

Also, at the delegate method "didFinishPickingMediaWithInfo" there is no entry of "UIImagePickerControllerEditedImage" in the "info"-NSDictionary.

What i did:

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

#if TARGET_IPHONE_SIMULATOR
    // iPhone-Simulator has no camera, just save images to the photoalbum on the simulator to use them here
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
#else
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif

    imagePickerController.editing = YES;
    imagePickerController.delegate = (id)self;
    imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;


    [self presentModalViewController:imagePickerController animated:YES];

and the delegate method:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        // .... do something 
}

At this point, only 3 Keys are available in the Dictionary with the name "info":

  • UIImagePickerControllerOriginalImage
  • UIImagePickerControllerMediaMetadata
  • UIImagePickerControllerMediaType

nothing else. Also, no cropping window or any similar controls appeared.

Am i wrong with my expectation about the behaviour of the ImagePicker? At this SF-Question the user wrote "Crop window comes up". Was the feature removed? Is the documentation outdated or am i completely wrong here?

I also tried this code taken from another answer to a similar question:

- (void)    imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    [picker dismissViewControllerAnimated:YES completion:^{
          // Edited image works great (if you allowed editing)
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
          // AND the original image works great
        UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
          // AND do whatever you want with it, (NSDictionary *)info is fine now
        UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
    }];
}

No cropping window appears, the key "UIImagePickerControllerEditedImage" is not present at the info-NSDictionary.

Can someone explain me the right behaviour? Thank you very much in advance.

like image 633
itinance Avatar asked Jun 10 '14 19:06

itinance


Video Answer


1 Answers

this one is works for me:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *originalImage, *editedImage;

    editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
    originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

    if (editedImage)
        self.profilePictureImageView.image = editedImage;
    else
        self.profilePictureImageView.image = originalImage;

    [picker dismissViewControllerAnimated:YES completion:nil];
}
like image 99
Anthony Marchenko Avatar answered Oct 23 '22 03:10

Anthony Marchenko