views:

308

answers:

1

This app i'm working on allows the user to take pictures, and saves them locally (a small thumbnail image via Core Data, and the full-size image in the documents directory). I'm finding that writing the image file to the documents directory takes a long time, though -- 8 seconds on my iPhone 3GS, even longer on my iPhone 3G and first-gen iPod touch. I write the file this way:

[imgData writeToFile:imagePath atomically:YES];

... is there a faster way to do this? The iPhone's camera app seems to write the images to the filesystem very quickly. I could do the writing in a thread, but i'm concerned about the user possibly quitting my app before the thread finishes.

+2  A: 
  • A closely related question has been asked before, and the general opinion is that the iPhone's camera app uses undocumented internal API calls. I'm personally not sure if this is true, I think it's more likely that the iPhone app can persist in the background until the image is saved, a "commodity" not yet available to third party apps on non-jailbroken devices.
  • Perhaps you should try indeed using a thread to save your image, but in a more data-oriented way, in byte chunks. This way, if the user presses the home button, in your - (void) applicationWillTerminate(UIApplication *)application method from the app's delegate, you could finalize the process of data saving to disk. This will only buy you an extra 5 seconds, though, but it's better than nothing at all. Similar recommendations have been given on saving a text file. Hope this helps, best of luck!
luvieere
The writeToFile:atomically option is better than writing out small chunks at a time, because you know you'll either have a whole file or not... the background thread is a good idea.
Kendall Helmstetter Gelner
Do consider taking advantage of the extra 5 seconds in applicationWillTerminate.
zaph