获取所有文件形成一个组文件夹? [英] Get all files form a group folder?
问题描述
经过长时间的挣扎,我无法做最简单和最明显的事情,从我在我的应用程序中通过添加新文件夹"创建的特定文件夹(组)中读取所有文件/照片组"菜单在 Xcode 的左侧.
我试了这么多段代码,都说没有那个文件夹,但是文件夹在我面前.
if let path = Bundle.main.resourcePath {让 imagePath = 路径 + "/Artworks"让 url = NSURL(fileURLWithPath: imagePath)让 fileManager = FileManager.default让属性 = [URLResourceKey.localizedNameKey,URLResourceKey.creationDateKey, URLResourceKey.localizedTypeDescriptionKey]做 {let imageURLs = try fileManager.contentsOfDirectory(at: url as URL, includesPropertiesForKeys: properties, options:FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)打印(图片网址:\(图片网址)")//从 URL 创建图像//var myImage = UIImage(data: NSData(contentsOfURL: imageURLs[0])!)} catch let error1 as NSError {打印(错误1.描述)}}
<块引用>
无法打开文件Artworks",因为没有这样的文件."
我还应该怎么做才能在一个组文件夹中排列大量照片并仅从该文件夹中读取所有照片名称/网址?
打印路径给了我:
/Users/me/Library/Developer/CoreSimulator/Devices/673ED39D-61D6-4F2D-BAC9-3EC7C4AC2089/data/Containers/Bundle/Application/7CDD3B62-B04O-4A48-8716-DA5AppName.app8/艺术品
在 Xcode 项目中添加图像时必须创建文件夹引用.
[![文件夹参考][1]][1]
创建文件夹引用后,它将如下所示.
[![项目导航器][2]][2]
可以使用以下代码读取文件.
let directory = Bundle.main.path(forResource: "iPad", ofType: nil, inDirectory: "1")让contentsArray = 试试?FileManager.default.contentsOfDirectory(atPath:目录!)
在这种情况下,contentsArray 的计数为 7.[1]:https://i.stack.imgur.com/oLkyD.png[2]:https://i.stack.imgur.com/Toizp.png
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
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.stack.imgur.com/oLkyD.png [2]: https://i.stack.imgur.com/Toizp.png
这篇关于获取所有文件形成一个组文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!