views:

105

answers:

1

I am writing an application that uses UIImagePickerController to take multiple pictures with the camera as fast as iOS allows. My application has to run on iOS 3.13 on all versions of iPhone hardware (v1 through 4). I am using UIImagePickerController with a cameraOverlayView.

My question is, how can I determine programmatically how many photos can be processed at once based on available memory? See below for more details. Any help is appreciated!

When the imagePicker delegate recieves imagePicker:didFinishPickingMediaWithInfo:, the delegate saves the full size image in the savedPhotosAlbum with UIImageWriteToSavedPhotosAlbum(). Once I have this problem solved, the app will be doing more things with the full size image.

As a test, I take pictures as fast as I can. Eventually, as expected, the app receives memory warning due to the large number of photos in memory while saving to the photo album. If I ignore the memory warnings and continue taking pictures, eventually the app is killed by iOS b/c of this.

I have no problem with setting a limit on the number of concurrent photos I allow the app to process at a time. Each iPhone HW version has different memory capacities and different camera resolutions. With iOS 4, multi-tasking may also effect the available memory. I'd rather do this programmatically than hard-code a limit based on HW version.

A test application that shows this behavior is at Source Code

A: 

It's easier to hard-code the limit.

There's no easy way to find out what the "available memory" is, since more memory may suddenly become free if there's a memory warning, or if the phone decides to kill various background apps (namely Safari). There's NSRealMemoryAvailable(), but that might just return the installed RAM (and is probably what [NSProcessInfo physicalMemory] uses).

You could try saving images in a queue — saving one image at a time is likely to use less memory. You can also add markers to your queue when you receive a memory warning, and disable image-saving when there's more than one (ideally you want to stop taking pictures if you get "level 2" or "level 3" memory warnings, but while these are printed to the console, I don't know any easy way of checking in code. Presumably you want to resume on a memory un-warning, but these don't exist either).

Also note that you can get the raw JPEG data (on some OS versions) through a notification; I forget what the name is, but the userInfo key is @"AVCaptureNotificationInfo_JPEGData". If you save the raw JPEG data, you might be able to process it later.

I think UIImageWriteToSavedPhotosAlbum() stupidly decompresses the JPEG returned by the camera and recompresses it, but I could be wrong. Camera images are only around 1-2 MB, so you ought to be able to take plenty without it crashing, but in my tests that's not the case.

tc.