Adding pictures to the Simulator

There is a limitation with Quartz when dealing with pictures. This problem is undetectable from the simulator, but its solution should be testable in it.


The limitation is that, some might have guessed, Quartz2D relying on OpenGL and thus the underlying hardware, it is impossible to render images greater than 1024×1024. Impossible to put them in views. Impossible to drawInRect: or drawAtPoint: them. What’s worse, images taken from the camera itself are larger than 1024×1024 (on the current generation iPhones, they are 2MP, or 1600×1200). Even more, the simulator accepts them and renders them correctly, so until you debug on the device itself, this bug is totally invisible.

Using the code of my last iPhone entry, you can resize them. Using a simple rule of three, the code can easily scale a UIImage to a max height or width keeping its ratio. That’s easy enough. But how can we test our newly bug-ridden software in the simulator? All pictures in its photo album are 800×600. Well, there is a simple way.

Start the simulator. Drag the image you want to add there, it will start safari and shows it to you. Tap and hold, then tap “Save Image”. Bingo, it’s now in the photo album.
There is another way for this. It is a bit more obscure, and it has a flaw. The official iPhone Simulator directory is ~/Library/Application Support/iPhone Simulator/User/. Inside is the whole file system of the simulator, and particularly the Media/ directory is interesting. It contains a DCIM directory, which format will be recognized by digital cameras aficionado. In fact, it is the exact same format… if you have a camera that uses THM files.
Your pictures have to be in JPG format. They have to be sequentially numbered IMG_0001.JPG, IMG_0002.JPG, IMG_0003.JPG, etc. Ideally, they have to have a THM file with them. If not (and this is the flaw), then you won’t be able to see a thumbnail when browsing your album. Fortunately, this THM file is just an image, so you could copy a 32×32

If you have 1 or 2 pictures to add, use the first procedure. If you have a thousand that you’d like to put in there, I’d suggest this script.

#!/bin/bash
simPath="$HOME/Library/Application Support/iPhone Simulator/User/Media/DCIM/100APPLE"
 
if [ -z "$1" ]; then
   echo usage: $0 "<folder>"
   exit 1
fi
 
if [ ! -d "$simPath" ]; then
   echo $0: path $simPath not found.
   exit 1
fi
 
# Find out which incremential number we're at currently.
index=1
for i in `ls $1/*.{jpg,png,gif,bmp} 2>/dev/null`; do
   while [ -f "$simPath/`printf IMG_%04d.JPG $index`" ]; do
      let index=$index+1
   done
 
   jpgName=`printf IMG_%04d.JPG $index`
   thmName=`printf IMG_%04d.THM $index`
 
   echo $i "->" $simPath/$jpgName
 
   sips -s format jpeg $i --out "$simPath/$jpgName" > /dev/null 2> /dev/null || continue
   sips -s format jpeg -z 96 96 $i --out "$simPath/$thmName" > /dev/null 2> /dev/null || continue
 
   let index=$index+1
done

It takes all images from a directory (conveniently passed in argument) and save it to the simulator. Just put this in a .sh file and execute it from the terminal.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Print this article!
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

Comments (6)

Picture 15 v1.0 - apps.innobec.comNovember 7th, 2008 at 11:47 am

[...] was a bug in there that was undetectable from the simulator. I’ve posted it here on my personnal-technical [...]

[...] OfCodeAndMen();

iWyreNovember 25th, 2008 at 9:49 am

[...] Of Code and Men blog has a bash script that will functions for copying the files onto the simulator. Edit: I’ve been told that [...]

[...] How to add photos? Apple – Support – Discussions – Recovering / Adding Photos to Iphone … OfCodeAndMen();

[...] it will open (or at least attempt to) the file in Safari. It works for images – very useful for adding images to the PhotoLibrary. I’ve just tried it on a PDF, text, and HTML file, and they all worked as well. A lot easier [...]

[...] There’s a good post about this on ofCodeAndMen() [...]

Leave a comment

Your comment