UIImagePickerController cropRect seems to be useless?

June 6th, 2009 § 12 comments

[UPDATE: In iPhone OS 3.0, the cropRect behaves more consistently (even if it’s still a little odd), see Stormy Productions’ Image Picker Sample, which works perfectly in 3.0. This article only applies to 2.2.1.]

I am using UIImagePickerController to let the user choose an image from their photo library, and allowing image editing (move and scale).

When the user is done moving and scaling, the OS calls my UIImagePickerControllerDelegate‘s didFinishPicking method with information about the original image and the cropping rect in editingInfo.
move and scale
One might think that the cropping rectangle returned by the UIImagePickerController would be in the coordinate system of the original UIImage, since that would make the most sense. One would be wrong, however. Based on my own experiments on my iPhone and various threads on Apple message boards, it seems that sometimes the cropping rectangle is based on a 640×480 image (that is: the original photo is first resized to 640×480 pixels, and the cropRect is in the resized image’s coordinate system).

But not always! Screenshots taken on iPhone (using power button + home button), which are 320×480, have a cropRect based on the original image size.

So that’s easy to deal with then, right? Just compare the original image’s size to see if it’s 320×480 or not…

Not so fast. That does not always work. I have on my iPhone some 320×480 images that, when picked with the image picker, return cropRects that are in some weird coordinate system. An unzoomed (1:1) crop returns a width and height of 432×433! That would imply the image is scaled to 432×648 before cropping. Odd!

(Where did these images come from? Both of them were created by other iPhone apps, saving a 320×480 image to the photo library. UPDATE: it seems to happen with JPEGs of certain sizes/proportions. See my comments for sample images.)

Unfortunately, so far I have been unable to distinguish between these two types of 320×480 images in code. Without being able to get reliable info about the coordinate system of the cropping rect, the cropping rect is basically useless. (And no, you can’t just compare the cropRect to see if it’s 432 pixels wide :P Once you scale that picture in the image picker, the width could be anything, still in that weird coordinate system.)

If anyone has any code that reliably deals with all images (including images of arbitrary dimension [e.g 527 x 325] that are saved to the iPhone), at all zoom levels, please let me know :)

Tagged ,

§ 12 Responses to UIImagePickerController cropRect seems to be useless?"

  • Hi,

    Not sure if you’ve seen this thread on the Apple dev forums, but I found it helpful in dealing with all the strangeness you describe:


    It’s not pretty and is a surprising amount of work just to get the proper scaled and/or cropped image the user specified.

    I used the code posted there as a start and finally got something working after lots of trial and error a few months ago.

    That bad news is a recent post says this doesn’t work the same on 3.0. Ug!

    I haven’t verified that myself yet… I really don’t want to have to revisit that code.

  • bunnyhero says:

    I’m looking at that now… I will have to try it. At first skim, though, I don’t see how it could handle the odd 320×480 images I describe in my post. I will have to actually try it out, though.

  • Here’s what I was able to figure out. I’m not 100% certain this is correct, but seems to match all the test images I tried:

    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.

    I have an ImagePicker class I created that might help. If you’d like I can share the code with you. I’d be curious if it handles the problems you are seeing.

  • bunnyhero says:

    Hmmm… interestingly, I just realized that the two images that were returning weird crop references were both created by the iPhone app “If Found+” from Polka (although the newest version of the app instructs the user to take a screenshot, rather than saving it itself).

    I wonder if I should just ignore that weird edge case.

  • bunnyhero says:

    Hmm, it seems any 320×480 JPEG (rather than PNG) triggers the odd scaling of the cropRect. I’m going to try some other sizes of JPEG.

  • bunnyhero says:

    Ok. Try any of the images here on an actual device and your ImagePicker code. I can’t seem to figure out how iPhone decides what to use as the basis for its cropping rect.


    (I just saved each image to my iPhone from Mobile Safari.)

  • You ever solve this problem? I just tested your images with my code and best I can tell it appears to handle them properly. My code is just trying to get the current crop of the selected photo and use it as a background image, so it’s taking the cropped image and just redisplaying it on the screen. From what I can see it look sto be doing it properly with your images.

    Sorry for the delayed reply. Been swamped with work all week. Today’s the first day this week where I wasn’t working 15 hours a day.

  • bunnyhero says:

    So your code is taking the original image and doing the crop itself? That is, not using the first image paramater that the UIImagePickerController sends you. If so, that’s awesome. Is this on 3.0 or 2.x?

  • Yes, I’m not using the image returned by the UIImagePickerController, I’m using the crop info to re-crop the original image. At least, that’s what I remember. I’m pretty sure the image returned was lower res or was not full aspect ratio, so I didn’t like it.

    It’s been several months since I wrote the code (based on that code in the post from the Apple dev forums), so my memory is a little fuzzy, but you are welcome to have a copy.

    The code should work on both 3.0 and 2.x.

  • bunnyhero says:

    That would be really awesome if you could share it :) Thanks! I just realized my email address isn’t on here. I can be reached at gmail.com (bunnyhero) or wayne at bunnyherolabs.com.

  • Hi, sorry for the delay – it seems weekends are the only free time I have lately for my hobby iPhone stuff. The rest of the week is working on freelance iPhone dev projects.

    Here’s a link to a new blog post with link for Xcode project including sample ImagePicker class.


  • bunnyhero says:

    Thanks Brian! Downloading now… :)

What's this?

You are currently reading UIImagePickerController cropRect seems to be useless? at bunnyhero dev.