I am trying to use UIImagePickerController
with UIImagePickerControllerSourceTypePhotoLibrary
, but it says, "No photos". Where does the simulator get the images from? Where should I copy the images so that they are displayed in the simulator?
30 Answers
The simplest way to get images, videos, etc onto the simulator is to drag and drop them from your computer onto the simulator. This will cause the Simulator to open the Photos app and start populating the library.
If you want a scriptable method, read on.
Note - while this is valid, and works, I think Koen's solution below is now a better one, since it does not require rebooting the simulator.
Identify your simulator by going to xCode->Devices, selecting your simulator, and checking the Identifier value. Or you can ensure the simulator is running and run the following to get the device ID xcrun simctl list | grep Booted
Go to
~/Library/Developer/CoreSimulator/Devices/[Simulator Identifier]/data/Media/DCIM/100APPLE
and add IMG_nnnn.THM and IMG_nnnn.JPG. You will then need to reset your simulator (Hardware->Reboot) to allow it to notice the new changes. It doesn't matter if they are not JPEGs - they can both be PNGs, but it appears that both of them must be present for it to work. You may need to create DCIM if it doesn't already exist, and in that case you should start nnnn from 0001. The JPG files are the fullsize version, while the THM files are the thumbnail, and are 75x75 pixels in size. I wrote a script to do this, but there's a better documented one over here(-link no longer work).
You can also add photos from safari in the simulator, by Tapping and Holding on the image. If you drag an image (or any other file, like a PDF) to the simulator, it will immediately open Safari and display the image, so this is quite an easy way of getting images to it.
Explain step by step of Airsource Ltd's answer for adding image to simulator:
- Drag it to simulator, then Safari opens (or browse to the Image in the internet using Safari)
- Hold your click on the image
- When the pop-up appears, choose Save Image and enjoy ;)
Update: for iOS Simulator 4.2, do these steps twice to get it work. Thanks kevboh
!
Update: This also works for iOS Simulator 6.1
Since Xcode 6 you can use the command line tool xcrun simctl
.
Usage is very simple; to add a photo to the currently running simulator you use the booted
placeholder.
xcrun simctl addmedia booted ./MyFile.jpg
To add it to any other simulator, you use its device id, which can be found by running xcrun simctl list
.
xcrun simctl addmedia E201E636-CE6C-11E5-AB30-625662870761 ./MyFile.jpg
Its simple. Just follow these steps :
- Drag and drop image onto Simulator
- Now image opens into Safari browser (file://.../ImageName). Tap-and-hold on the Image.
This displays actionSheet with Save, Cancel option (Also copy in case of iOS 7 simulator).
Save the image. The image gets added into Library.
I just stumbled upon how to bulk upload images on the iOS Simulator. (I've only confirmed it on 6.1.)
Backup the folder:
~/Library/Application Support/iPhone Simulator/6.1/Media
Copy all your images into the folder:
~/Library/Application Support/iPhone Simulator/6.1/Media/DCIM/100APPLE
Move or delete the folder:
~/Library/Application Support/iPhone Simulator/6.1/Media/PhotoData
Restart iOS Simulator
Open the Photos app
The simulator will restore all the images from the 100APPLE folder!
For iOS 7 I did the following:
copy photos to these two folders:
~/Library/Application Support/iPhone Simulator/6.1/Media/DCIM/100APPLE
~/Library/Application Support/iPhone Simulator/7.0/Media/DCIM/100APPLE
delete these 4 files only (to avoid duplicates on relaunch):
~/Library/Application Support/iPhone Simulator/6.1/Media/PhotoData/Photos.sqlite-shm
~/Library/Application Support/iPhone Simulator/6.1/Media/PhotoData/Photos.sqlite-wal
~/Library/Application Support/iPhone Simulator/7.0/Media/PhotoData/Photos.sqlite-shm
~/Library/Application Support/iPhone Simulator/7.0/Media/PhotoData/Photos.sqlite-wal
Method 1 (Easiest Way): If you have your image on Mac
You can drag an image from the Finder on your Mac to Simulator, and it is saved to the Saved Photos album.
Method 2: If its on any URL
To save an image from a webpage to the Photos app
- Place the pointer on the image you want to save, and hold down the mouse button or trackpad.
- When the menu appears, click Save Image to save the image to the Photos app in an iOS simulator.
- The image is saved to the Saved Photos album in the Photos app.
Incase someone looking for Apple Documentation regarding Copying and Pasting in Simulator.
None of the answers had the exact solution that I needed.
The steps I've found for myself working on iOS 5.0 and above simulator are as follows:
Close the simulator if it is running xcode project.
Run simulator from the dock by clicking on it.
Drag & drop the image into simulator.
Tap and hold the image (opened in safari) and select the save option.
You are done.
If you need to import more than just one or two photos then take a look at this article that I wrote. It describes an easy way to perform a bulk import of photos and works for iOS 4.x.
For iOS 8, If there is no need to retain photo capture date and location, just drop photo files to the simulator.
To retain photo meta data, do the following:
- Copy photo files to: /Users/{USER}/Library/Developer/CoreSimulator/Devices/{UDID}/data/Media/DCIM/100Apple
- Remove (or rename) folder: /Users/{USER}/Library/Developer/CoreSimulator/Devices/{UDID}/data/Media/photoData
- Relaunch Simulator
Note: You need to replace {USER} with your user name and {UDID} with the UDID of the simulator. To find UDID for your simulator, from Terminal, run 'xcrun simctl list'.
If you can not drag and drop your files because you experience the error:
One or more media items failed to import: : The operation couldn’t be completed. (PHPhotosErrorDomain error -1.)
Move your files into the Documents
folder and then drag them into the simulator. This will trigger the simulator to ask for permissions to access your files. Having them inside the Downloads
folder, will not.
try this app I've made. download the code and run it in simulator https://github.com/cristianbica/CBSimulatorSeed
In OS X Catalina with Xcode 11 you have to do this differently.
First you MUST start the simulator. If you want to copy, say a photo to MULTIPLE simulated devices. Start all of them up. Then Right Click on a SINGLE (Multiple images will fail. ONLY does a single file.).
Share -> Simulator
A 'share sheet will pop-up. You must choose an active simulator in the combo box and hit send.
It will send one to many but NOT many to many of selected photos.
I hope this helps folks. Drag and drop was supported in the last versions of Xcode and OS X but not with OS X Catalina and Xcode 11.
While this IS in the directions it currently IS NOT working.
What DID work for me was to first import my images into iPhoto on OS X and then DRAG/DROP them from my OS X iPhoto and drop into the simulator. It would appear the drag/drop for photos into the simulator is CURRENTLY only working from OS X iPhoto. :-(
I wrote a bash script to do this. Check the link[1]
#!/bin/bash
# Imports pictures into all iOS simulators.
path_to_pic="src/ios/pictures/"
mkdir -p /Users/$(whoami)/Library/Application\ Support/iPhone\ Simulator/{5.0,5.1,6.0,6.1}/Media/DCIM/100APPLE/
find ~/Library/Application\ Support/iPhone\ Simulator/ -type d -name '100APPLE' -exec cp /Users/$(whoami)/$path_to_pic/* {} \;
I just needed some random images for testing, so this is how I did it.
I have the simplest solution in the world. Just open Safari in the simulator, go to Google images (or your own web or Dropbox URL), view an image, hold down the mouse button for 2 seconds, and you'll see "Save Image" - it will save right into the Photos library. Rinse and repeat.
With the drag and drop feature you will lose all the metadata of the photos.
I've created a project that make it really easy to import assets to the simulator: MBAssetsImporter.
It enables you to import both videos and photos while preserving all the original metadata of the files.