Bad_Developer
Bad_Developer

Reputation: 537

Load image from folder inside project


I'm making an app with the folder like image below:
enter image description here
As you can see in this picture, Stickers folder have 2 sub folder "1" and "2". In side them is bunch of icon and I wanna load it in a collection view with each sub folder is a package of different sticker. For more details, please see this picture: enter image description here
So, how can I get it in my project folder? I've read about access document directory but seem like it not solve my problem.
Please help me. Thanks in advance.

Upvotes: 3

Views: 6413

Answers (2)

shallowThought
shallowThought

Reputation: 19602

Look at Filemanagers API. It offers all you need.

Example to get content at path:

let pathToDir1 = Bundle.main.resourcePath! + "/Stickers/1";
let fileManager = FileManager.default
let contentOfDir1 = try! fileManager.contentsOfDirectory(atPath: pathToDir1)

Example to iterate over:

let docsPath = Bundle.main.resourcePath!
let enumerator = fileManager.enumerator(atPath:docsPath)
let images = [UIImage]()
while let path = enumerator?.nextObject() as! String {
    let contentOfCurDir = try! fileManager.contentsOfDirectory(atPath: path)
    // do whatever you need.
}

For details see Apples documentation and sample code.

Upvotes: 4

Jim Tierney
Jim Tierney

Reputation: 4105

If you're looking to access an image within your project folder you simply need to use the image name. You don't need to define the whole path.

Objective-C

UIImage *img = [UIImage imageWithName:@"IMAGE_NAME"];

swift

var img = UIImage(named:"IMAGE_NAME")

Upvotes: 3

Related Questions