Bronze challenge solution


#1

Pretty straight forward (but took me an annoyingly long amount of time to figure out)

- (IBAction)takePicture:(id)sender
{
    ...
    imagePicker.allowsEditing = YES;
    ...
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    ...
    UIImage *image;
    
    if (info[UIImagePickerControllerEditedImage]) {
        image = info[UIImagePickerControllerEditedImage];
    } else {
        image = info[UIImagePickerControllerOriginalImage];
    }
    ...
}

#2

With allowsEditing enabled, an image seems always set with ‘EditedImage,’ so the line below looks enough.

    UIImage *image = info[UIImagePickerControllerEditedImage];

#3

Hey all,

This was exactly my solution, unfortunately nothing changes in the app. The app allows me to select an image from the photo library as before, select that photo, but no other controls or options for editing are presented in the UI.

Is this because I’m using the simulator, the camera isn’t detected on the device, and I’m choosing images from the Saved Images of the simulator? Is editing only available for photos that are taken with the device? To be clear, I’m not having a problem accessing the edited image (by setting allowsEditing, you always get back an “edited” image, even if no edits have taken place). Rather, no UI is being presented in the imagePicker which allows for any editing. I’ve not included code because it is only two lines and identical to what has been posted previously. Yes, editing is allowed. Yes, I’m retrieving the edited image from the info dictionary. There is just no user interface on screen to perform any editing tasks.

What am I missing here?


#4

I think you’re problem is that you perhaps expect more from the editing functionality. For me, with editing enabled, all I can do is hold ALT as I drag to zoom in and out of the image and then pan the zoomed image accordingly. Without editing enabled, I can’t zoom in or pan, so I think that’s all there is to it.


#5

This is correct. allowsEditing currently is limited to just cropping. It’ll work with whatever source type your specify.


#6

Thanks all. It wasn’t clear to me what controls editing was supposed to give me. I actually didn’t even try to zoom and pan (not something you immediately think to do in the Simulator). I just stared dumbly at the lack of edit widgets (or handy instructions, etc) and thought “Well that isn’t right.”


#7

I have basically the same code as monkey boy and the app editing seemed to work fine. However, my question follows on from those from sunny4s and InCodeEncode. I was expecting that by flagging the edited versus original versions of the image through the conditional if-else statement that if I didn’t edit the image the method would return the original image… however only edited images are returned regardless of whether I actively edited the image or just clicked ‘Choose’.

So how do I return an original image when the user has performed no editing, and an edited version where she/he has performed edits (e.g. zoom, pan, scroll etc.)? (Here I was thinking via the UIImagePickerController itself rather than via using touchesBegan etc. while editing).


#8

I second curiOS’s question - when I pick a photo that is “taller” than the crop window is with allowsEditing = YES, you are forced to crop the image. Pinching down to smaller doesn’t work it just jumps back-up to fill the width (thus forcing a crop of some of the top and/or bottom content). There does not seem to be any way for the user to indicate to the picker “er…thanks for the crop option but I don’t want to crop”. Is there some way to adjust the crop lines (I am in the simulator)? If not I’m not sure this it is solvable outside of implementing “fully-customizable” image capture and then implementing more controls.


#9

Unfortunately, yes.

One of the keys returned in the info dictionary is UIImagePickerControllerCropRect. You cannot set it, but you can verify what was used:

[code] CGRect cropRect = [info[UIImagePickerControllerCropRect] CGRectValue];

NSLog( @"cropped using rect with origin (%.2f,%.2f), size (%.2f x %.2f )",
      cropRect.origin.x, cropRect.origin.y,
      cropRect.size.height, cropRect.size.width
      );

[/code]
Apple uses the same technique as the book for selecting either the edited image or the original image, though they note that the original image is always available.


#10

Hi,
Dumb question, but, what editing is done on an actual device - just cropping? If so, how do you do it?
Thanks