47

I have photos on my Mac that I would like to add to the iPhone Simulator to test my application.

In other words: how do I add photos to the iPhone Simulator?

Edit : What about iphone Simulator 4.0 ? iphone Simulator 3.0 & 4.0 both working differently.

Thanks in advance for helping me...

sagarkothari
  • 24,520
  • 50
  • 165
  • 235
  • 1
    Answer works for iOS7, it's the duplicate but not the accepted answer: http://stackoverflow.com/a/19845346/317461 – mahal tertin Feb 12 '14 at 13:44

8 Answers8

91

Open the Window in mac where your images are stored.

Open your simulator another side.

Now drag your image from mac window to simulator,

simulator will open safari, and in a safari tab your image will be shown.

Tap & press down on image in simulator,

There will be message to "save image",

save image.

It will be added to your iPhone simulator.

Edit :

First just look at following image.

alt text

In iPhone simulator 4.0 ( iphone/iphone simulator ), itself it maintains a sqlite database for added images. So, if you want copy paste system - first make insert entries & then copy paste. That would be ridiculous way.

Ok. Let me explain simpler way of doing it.

  • open the finder in which you have bulk images that you want to add in simulator.
  • drag & drop first image into iphone simulator
  • on image - tap & hold for 1 second.
  • action sheet will appear - tap on save option
  • repeat same process for all images
  • this will do sqlite entries also.
  • now, open the /Users/YourUserName/Library/Application Support/iPhone Simulator/4.0
  • make a back up of Media directory. ( for example copy it & paste it on desktop )
  • when you reset your iphone simulator, all images will be gone
  • you need not to repeat all the process again, you have back up of it.
  • just copy & paste from back up to 4.0

alt text

Spooky
  • 2,966
  • 8
  • 27
  • 41
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
17

A more easy to understand version of sagar's answer:

Open a Finder window to where your images are stored and the iPhone Simulator. Then drag the images from the Finder window into the simulator. The simulator will open Safari with your image. Click and hold to save the image to the iPhone camera roll. You can now use those images as you normally would.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
10

I had the same question recently. The drop-the-photo-on-Safari approach works well enough if you're doing one at a time. For several images at once, I found a great blog post that explains where the simulator finds it's images.

The same poster links to a bash script to load a directory of images into the simulator. I now use this to reset my simulator's environment to a known good state as part of my build. That way, I can keep my images together with my code instead of depending on the state of the simulator's directory tree.

EDIT The original script was on a server that seems to be gone. I've pasted it here with the change needed for iPhone SDK 3.0. Credit for the script properly goes to the author of "Of Code and Men".

#!/bin/bash
simPath="$HOME/Library/Application Support/iPhone Simulator/User/Media/DCIM/100APPLE"
thmPath="$simPath/.MISC"

if [ -z "$1" ]; then
  echo usage: $0 "<folder>"
  exit 1
fi

if [ ! -d "$simPath" ]; then
  mkdir -p "$simPath"
  mkdir -p "$thmPath"
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 "$thmPath/$thmName" > /dev/null 2> /dev/null || continue

  let index=$index+1
done
  • 1
    the code provided has one major problem.. files with spaces will not be handled correctly. Change the _for_ loop to: `for i in "$@"; do` And run it using `find ./photos -name "*.jpg" -exec ./load-photos.sh {} +` where your images are in _photos_ and the script above is saved as _load-photos.sh_ – ohhorob Dec 06 '09 at 23:39
  • 1
    Alternatively, change the whole `for i in ...` line into: `find "$1" -iname "*\.jpg" 2>/dev/null | while read i; do` and the script will work as advertised. – Pascal Jun 15 '10 at 15:54
  • What about iPhone simulator 4.0 ? I have edited my question & answer. Please check it out & let me know if any comments. – sagarkothari Jul 07 '10 at 04:19
  • According to [this answer](https://stackoverflow.com/a/25758880/539599), it may be easier to use `xcrun simctl addphoto`. – Raphael Aug 02 '17 at 17:00
9

For a few images:

  • Add image files to folder->
  • Add folder to xcode project->

(this will keep everything together for this project and folder can be removed from project at your discretion)

  • open xcode and simulator in same window->
  • open "photos app" in simulator->
  • drag and drop from xcode to simulator->

(image(s) will open in safari)

  • click and hold image in simulator->
  • chose save->

Image(s) will now be in the 'photos app' album in simulator. This was quickest and cleanest way for me.

Peter Oram
  • 6,213
  • 2
  • 27
  • 40
Hblegg
  • 189
  • 2
  • 10
1

I've been using an automated technique I originally found at iPhone Dev SDK. Include the images in your app's resources, then:

UIImage *image = [UIImage imageWithName:@"imageName.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

http://www.iphonedevsdk.com/forum/iphone-sdk-development/2225-no-photos-in-iphone-simulator-how-to-add-photos.html

slothbear
  • 2,016
  • 3
  • 21
  • 38
1

3 Simple Steps

1) Drag & Drop image onto simulator
- this will open a browser with your image
2) Click & hold image
- this will open options
3) save image
- this will copy image onto simulator
Watch YouTube Video ( add images to iphone simulator)

user380427
  • 1,329
  • 1
  • 8
  • 2
1

I've edited the script to create a structure that works with the iPhone 4.0.1 simulator. On my system I have subfolders for iphone simulator 3.2, 4.0 and 4.0.1. So I've kept the original output of the script and modified the structure as it needs to create a PhotoData folder for the thumbnails. After running the script I create sym links for the 3.2 4.0 and 4.0.1 simulator directories as follows:

ln -s $HOME/Library/Application\ Support/iPhone\ Simulator/User/Media/DCIM/ $HOME/Library/Application\ Support/iPhone\ Simulator/4.0.1/Media/DCIM
ln -s $HOME/Library/Application\ Support/iPhone\ Simulator/User/Media/PhotoData/ $HOME/Library/Application\ Support/iPhone\ Simulator/4.0.1/Media/PhotoData

future runs of the script will update all simulators. Here's the modified script:

#!/bin/bash

rootPath="$HOME/Library/Application Support/iPhone Simulator/User/Media"
relPath="100APPLE"
simPath="$rootPath/DCIM/$relPath"
thmPath="$rootPath/PhotoData/$relPath"

if [ -z "$1" ]; then
  echo usage: $0 "<folder>"
  exit 1
fi

if [ ! -d "$simPath" ]; then
  mkdir -p "$simPath"
  mkdir -p "$thmPath"
fi

echo "Finding pictures..."
# Find out which incremential number we're at currently.
index=1
for i in `ls $1/*.{JPG,jpg,png,gif,bmp} 2>/dev/null`; do
  echo "considering $i..."
  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
  echo $i "->" $thmPath/$thmName

  sips -s format jpeg $i --out "$simPath/$jpgName" > /dev/null 2> /dev/null || continue
  sips -s format jpeg -z 96 96 $i --out "$thmPath/$thmName" > /dev/null 2> /dev/null || continue

  let index=$index+1
done
Cliff
  • 10,586
  • 7
  • 61
  • 102
1

I've been updating the script to try and add enough data to the SqlLite databases. As soon as you save a photo from safari the following data is entered:

sqlite> select * from Photo;
primaryKey|type|title|captureTime|width|height|userRating|flagged|thumbnailIndex|orientation|directory|filename|duration|recordModDate
43|0|IMG_0037|320336214.0|640|427|0|0|0|1|DCIM/100APPLE|IMG_0037.JPG|0.0|320336214.933387
sqlite> select * from PhotoExtras;
primaryKey|foreignKey|identifier|sequence|value
142|43|1|-1|DCIM/100APPLE
143|43|2|-1|IMG_0037.JPG
144|43|3|-1|76616
145|43|6|-1|
        streamtyped???@???NSMutableDictionary
146|43|7|-1|286
147|43|8|-1|
        streamtyped???@???NSValue
148|43|9|-1|8252
149|43|10|-1|1
150|43|13|-1|0

The Photo table data seems quite simple although I'm currently assuming captureTime is not relevant. The PhotoExtras stuff is a bit more involved and seems necessary as my current script, which needs image magicks identify command, is not working.

identifier 1 is directory name identifier 2 is jpg name identifier 3 is file size in bytes

If anyone can help with the others..

The script as it stands is below:

#!/bin/bash

rootPath="$HOME/Library/Application Support/iPhone Simulator/4.2/Media"
relPath="100APPLE"
simPath="$rootPath/DCIM/$relPath"
sqlDB="$rootPath/PhotoData/Photos.sqlite"

if [ -z "$1" ]; then
  echo usage: $0 "<folder>"
  exit 1
fi

if [ ! -d "$simPath" ]; then
  mkdir -p "$simPath"
fi

echo "Finding pictures..."
# Find out which incremential number we're at currently.
index=1
for i in `ls $1/*.{JPG,jpg,png,gif,bmp} 2>/dev/null`; do
  echo "considering $i..."
  while [ -f "$simPath/`printf IMG_%04d.JPG $index`" ]; do
    let index=$index+1
  done

  imgName=`printf IMG_%04d $index`
  jpgName=`printf IMG_%04d.JPG $index`
  thmName=`printf IMG_%04d.THM $index`
  jpgWidth=`identify -format %w $i`
  jpgHeight=`identify -format %h $i`

  echo $i "->" $simPath/$jpgName
  jpgSize=`stat -f %z $i`
  jpgDir="DCIM/100APPLE"

  sips -s format jpeg $i --out "$simPath/$jpgName" > /dev/null 2> /dev/null || continue

  sqlite3 "$sqlDB" "insert into Photo (title,width,height,directory,filename) values ('$imgName',$jpgWidth,$jpgHeight,'$jpgDir','$jpgName')"
  foreignKey=`sqlite3 "$sqlDB" "select primaryKey from Photo where title='$imgName'"`
  sqlite3 "$sqlDB" "insert into PhotoExtras (foreignKey,identifier,value) values ($foreignKey,1,'$jpgDir'); \
                    insert into PhotoExtras (foreignKey,identifier,value) values ($foreignKey,2,'$jpgName'); \
                    insert into PhotoExtras (foreignKey,identifier,value) values ($foreignKey,3,$jpgSize);"
  let index=$index+1
done

I'll edit if I get further, the next stage is identifying what the various .ithmb files contain. Hopefully some sort of stack of jpg thumbnails.

Diziet
  • 2,397
  • 1
  • 26
  • 34