One question that gets asked frequently in iPhone developer forums is, ‘How do I add photos to the Camera Roll in the iPhone Simulator?’ The most-quoted solution is to drag an image onto the simulator window. This will open it in Mobile Safari from where you can save it to the Camera Roll by tapping and holding on the image. This is fine if you just need one or two images, however, if you need to load a large number of photos into the simulator then this one-by-one approach is going to get very tired very quickly. We hit this problem while developing an app for a client that includes a custom photo browser and we needed to load 100s of photos during testing.
Other approaches involve copying image files directly into the Simulator under its Media directory at
[home]/Library/Application Support/iPhone Simulator/4.x/Media. However, copying images directly into the Simulator can lead to problems with missing thumbnails or images just not appearing at all in the Photos app or Image Picker.
Our solution was to write an iPhone app designed to run in the Simulator that performs a bulk import of image files from disk. The app leverages iOS support for saving photos to the user’s Camera Roll, which ensures that thumbnails are generated correctly and the Photo database is updated.
The Image Importer app
OK, so it’s not pretty, but it does the job. All you need to do is download the Xcode project at the bottom of this article, build it and run it in the iPhone Simulator.
Once the app is running just follow the instructions. Copy images that you want to import into the folder shown (jpg or png). The default location is a folder called ‘iPhone Images’ under your Pictures folder but you can change this if needed. Click the ‘Import’ button and wait. All being well, your images should now appear correctly in the Photo app. They will also be available through the Image Picker API and the new ALAssetsLibrary API in iOS4.0.
We’d love to hear if you have found this article useful – leave a comment.
Update: Some people are seeing a ‘Data Unavailable’ error. To fix this choose “Reset Content and Settings…” from the iPhone Simulator menu.
If you are still receiving the same error after this then:
- Drag and drop an image onto the simulator to open it in Mobile Safari
- Click on the image and hold
- Choose “Save Image”
- Open the Photo app in the simulator to rebuild your photo library
Robin Summerhill is a tech blogger, developer and architect. He is co-founder of Emu Analytics where he is currently working as Head of Technology.