Joon. P
Joon. P

Reputation: 2298

How to get directory of a folder inside xcode project? ios swift

I have bunch of tile images inside the folder called "Tiles"

I need to extract one image at a time from that folder, and set it to UIView object.

I am stuck on how to get the image from inside that folder.

Can anyone help do this in Swift?

enter image description here

Upvotes: 3

Views: 6768

Answers (1)

Leo
Leo

Reputation: 24714

If you have the image name,you can just access the image in this folder like this

let image = UIImage(named: "image.png")

Or this

let imageURL = NSBundle.mainBundle().URLForResource("image", withExtension: "png")
let image = UIImage(contentsOfFile: imageURL!.path!)

If you want get all images,you create a real folder inside the project

 let imageArray = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: "Titles") as! [NSURL]

This will return an urlArray of png inside Titles folder.You can use contentsOfFile as show before to access image

Upvotes: 5

Related Questions