1

I have an Xcode project which needs to access an image. My questions are:

  1. How to add images to the Xcode project. Do I need to follow a specific structure or convention?
  2. How do I access the image I added in step (a)

Online links say add bundle resources but none of them gives a conclusive answer showing what to add in Xcode project and how to access the same.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

4 Answers4

2

The best way to save image in your folder by creating an Asset Folder.

File -> New File -> Asset Catalog

enter image description here

Name the asset as image, it will create an asset folder image.xcassets

In this asset catalog, you will keep your images in specific folder.

you have to just write

imageView.image = UIImage(named : "MY_IMAGE_NAME") 
Ankur Lahiry
  • 2,253
  • 1
  • 15
  • 25
  • Thanks. My source tries to access the images from NSDocument directory of the selected iOS simulator. Is there any approch to move the files added in above asset catalog to the NSDocument directory? –  Oct 31 '19 at 03:39
  • So you want to add image to document directory manually and load this in your app? – Ankur Lahiry Oct 31 '19 at 03:45
  • I would like to package a set of images along with the xcode project and then move those images in document directory of selected iOS simulator. Is this feasible? –  Oct 31 '19 at 03:46
  • ```guard let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return true } print(URL(string: path))``` You will find a path, in mac `go to folder` with this path, keep your image in the folder, this is your simulator document directory. In iPhone, you won't get this and can set manually. – Ankur Lahiry Oct 31 '19 at 03:55
  • I am aware of the iOS simulator document directory. What i want to achieve is to pre-package set of images with the xcode project and then these images need to be moved around to "selected" iOS simulator's document directory automatically via code (without any manual intervention). –  Oct 31 '19 at 04:01
  • No, there is no way – Ankur Lahiry Oct 31 '19 at 04:02
  • Not sure if below link explains what i want to do https://stackoverflow.com/questions/6545180/ios-copy-a-file-in-documents-folder The pending question is how to add "Images" to resources folder in XCode –  Oct 31 '19 at 04:04
1

When you create a project,there would be a folder named Assets.xcassets.You can drag the image you need into the Assets.xcassets. And when you need to show the image,code this:

//the image name is `image.jpg`
let image = UIImage(named : "image") 
imageView.image = image

PS: You can also drag the image to the project directly,and the code:

//the image name is `image.jpg`
let image = UIImage(named : "image.jpg") 
imageView.image = image
Lynx
  • 381
  • 1
  • 13
1

You will be able to find a folder named Assets.xcassets in your project hierarchy. enter image description here

Drag and drop the image you wish to add into this folder.Here you can select 1X 2X or 3X size for your image. enter image description here

After doing this you will have to create an UIImage object to access it. for example:

let image = UIImage(named:"image_name")

Now you can use this image object wherever you wish to use the image.

Raj Mohan
  • 47
  • 1
  • 9
0

Whenever you are creating a project, you getting a folder Assets.xcassets. In this just add a New Image Set, add the name which one is your image name and drop here 1x, 2x & 3x here. OR you can create a separate Asset Cataolog.

  • My source tries to access the images from NSDocument directory of the selected iOS simulator. Is there any approch to move the files added in above asset catalog to the NSDocument directory? –  Oct 31 '19 at 03:41
  • what do you mean, i dont understand? –  Oct 31 '19 at 03:45
  • In NSDocument directory we use for to download the image. For development purpose required images stored on catalog – Sanjeev Mishra Oct 31 '19 at 03:45
  • My code tries to find the images in NSDocument directory of selected iOS simulator. How can i move the images in Assets.xcassets to selected iOS simulator NSDocument directory? –  Oct 31 '19 at 03:47
  • You can not move the files from NSDocument directory to Assess. – Sanjeev Mishra Oct 31 '19 at 03:48
  • Why do you want to move file from NSDocument directory to Assests? If do you want to show that image on your screen then get image from NSDocument directory through specified URL and convert it to in Data fromat and add on your screen. – Sanjeev Mishra Oct 31 '19 at 03:51
  • I am moving it the other way around. You are reading wrong. And i just want to place the images in NSDocument directory as such (no conversion) –  Oct 31 '19 at 04:07