Reputation: 1
After long struggling, I just can't do the most simple and obvious thing, to read all files/photos from a specific folder(group) that I create in my app by "Add new group" menu on the left side of Xcode.
I have tried so many pieces of code, all tells me there is no such folder, but the folder is there in front of me.
if let path = Bundle.main.resourcePath {
let imagePath = path + "/Artworks"
let url = NSURL(fileURLWithPath: imagePath)
let fileManager = FileManager.default
let properties = [URLResourceKey.localizedNameKey,
URLResourceKey.creationDateKey, URLResourceKey.localizedTypeDescriptionKey]
do {
let imageURLs = try fileManager.contentsOfDirectory(at: url as URL, includingPropertiesForKeys: properties, options:FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)
print("image URLs: \(imageURLs)")
// Create image from URL
//var myImage = UIImage(data: NSData(contentsOfURL: imageURLs[0])!)
} catch let error1 as NSError {
print(error1.description)
}
}
The file “Artworks” couldn’t be opened because there is no such file."
What else should I do to simply be able to arrange lots of photos inside a group folder and read only from that folder all the photos names/urls ?
EDIT : Printing the path gives me :
/Users/me/Library/Developer/CoreSimulator/Devices/673ED39D-61D6-4F2D-BAC9-3EC7C4AC2089/data/Containers/Bundle/Application/7CDD3B62-B04O-4A48-8716-D83F6EDA5689/appName.app/Artworks
Upvotes: 1
Views: 1335
Reputation: 2683
You have to create folder reference while adding the images in Xcode project.
[![Folder reference][1]][1]
Once folder reference is created, it will look like below.
[![Project navigator][2]][2]
The files can be read using the below code.
let directory = Bundle.main.path(forResource: "iPad", ofType: nil, inDirectory: "1")
let contentsArray = try? FileManager.default.contentsOfDirectory(atPath: directory!)
The contentsArray will have a count of 7 in this case. [1]: https://i.sstatic.net/oLkyD.png [2]: https://i.sstatic.net/Toizp.png
Upvotes: 4
Reputation: 1
Actually, amazingly, for 2 days no one could tell me the real issue here. Creating a group, is completely different from dragging a folder into the project.
For some reason, with Apple, its always complicated with files. I have to figure out the NOT so intuitive approach that a group that looks like a folder, is nothing but a nice way to look at something, and will not create a real folder accessible by the file manager.
Simply put, create a blue folder outside Xcode and drag it in.
Upvotes: -2