Saturday, June 20, 2009

Image Picker Sample

The UIImagePickerController provided by Apple is pretty weird. If all you want to do is grab an image, its usage is very straightforward. But, if you want to first allow the user to zoom and crop the photo, the way the controller works is very unintuitive.

Back in February I struggled with trying to use a UIImagePickerController to allow a user to make such cropping adjustments before having the image returned. Fortunately, I found a good starting point with a post on Apple's iPhone developer forum (read it here). Using that sample code and a lot of tinkering with many sample images, I came up with a method that seems to handle the zooming and cropping of arbitrary images.

Someone else was recently writing about how this process is a real pain in the neck, so I figured it might be helpful to others if I posted some sample code.

So, here's a link to a sample XCode project which implements a subclass of UIImagePickerController. The code isn't pretty - I wrote it in a hurry several months ago, it did what I needed at the time, and I don't want to revisit it now. Feel free to do whatever you'd like with it.

My key to understanding the whole image picker crop/zoom process and the cropping info returned is summarized by these comments in my code

"The crop rectangle returned has been scaled relative to the image being 480×640 regardless of the real image dimensions. And, to make matters worse, it’s a virtual 320×320 square centered on the screen rather than being the whole screen. So, we need to rescale the crop rectangle so it matches the original image resolution and so it’s the full screen."

Perhaps this will be useful or helpful to someone else.

The code works with both 2.x and 3.0 of iPhone OS, although the sample project file I include was made specifically for 3.0.



bunnyhero said...

Thanks for sharing this code! I have downloaded it and will take a look later.

bunnyhero said...

I tried your code and it works perfectly-- in OS 3.0. When running it on a 2.2.1 device, it exhibits the same erratic strangeness with certain images that I described in my blog post. So it appears to be an OS bug that Apple must have fixed in 3.0.

Tres Trantham said...

Thanks so much for this. I can not believe Apple would make this so convoluted. I did have a question regarding the 320×320 pixel mask over the image during crop and scale. Were you ever able to get this to change size? I realize the actual crop size is the screen size, but it still overlays the masked box. Either way, thanks again.

Anonymous said...

Thank you so much!! I was having trouble getting the second grab appear.

blue dot said...

Hi, Stormy.

Thanks for posting this, found it helpful for a project.

Question: Do you notice that the capture "shifts" down 20 pixels?

Is there a reason for that? And, if so, is there a place in the code that could be adjusted so it doesn't shift down, but instead stay "exactly" where the image capture/crop is?

Mostly Torn said...

Hi Blue Dot,

I han't noticed a 20 pixel shift, however I am no longer using this code. Apple cleaned things up a lot in the 3.x SDK and now the image picker API provides access to the cropped image without having to use all that extra code in this posting.

If you are using 3.x only, then I'd recommend just using the newer API from Apple.


blue dot said...


Thanks for responding.

That makes to decide on backward version support.

Such crispness...

prateek said...


Thanks for the code.

I am having a problem, please can you help.

In the code I made SOURCETYPE equal to UIImagePickerControllerSourceTypeCamera

When I run the application, I can take the photo from camera but after that, there are two buttons displayed "Retake" and "Use"

But none of the button are working when I tap them.

Also if I keep the debug point on "didFinishPickingImage", the debugger never comes to this point.

Please guide me what's the problem.

Thanks a lot.

All content copyright © 2009  Brian Stormont, unless otherwise noted.   All rights reserved.