xcode Swift:如何从资产中获取图像名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40344091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: How to get image name from assets
提问by Pan Mluv?í
I have 500mb of images in folders in assets. I don't know any of their names. Is there a way to access them...store them to Array and then display?
我在资产文件夹中有 500mb 的图像。我不知道他们的名字。有没有办法访问它们...将它们存储到数组然后显示?
Example: I have 3 images stored in Assets/folder1/folder2
folder 2 contains that 3 images..... I need to get those names.
示例:我在Assets/folder1/folder2
文件夹 2 中存储了 3 个图像,其中包含 3 个图像..... 我需要获取这些名称。
Here is the reason I need this.... Ive been provided with library of car images and that images have illogical names.
这就是我需要这个的原因......我已经获得了汽车图像库,并且这些图像的名称不合逻辑。
采纳答案by matt
You cannot do what you are describing while storing the images in the Assets Catalog. Fetching a resource from an Assets Catalog relies upon your knowing its actual name.
在资产目录中存储图像时,您无法执行您所描述的操作。从资产目录中获取资源依赖于您知道其实际名称。
Instead, store these folders directly at the top level of your app bundle. Now you have an actual folder that you can get a reference to, and can ask the FileManager for the names of the images in the folder.
相反,将这些文件夹直接存储在应用程序包的顶层。现在您有一个实际的文件夹,您可以引用该文件夹,并且可以向 FileManager 询问文件夹中图像的名称。
Example (Swift 2.2, sorry):
示例(Swift 2.2,抱歉):
let f = NSBundle.mainBundle().URLForResource("carPix", withExtension: nil)!
let fm = NSFileManager()
let arr = try! fm.contentsOfDirectoryAtURL(f, includingPropertiesForKeys: nil, options: [])
print(arr) // the URLs of the two jpg files
回答by Ksenya
Example above with Swift 3
上面使用 Swift 3 的示例
if let f = Bundle.main.url(forResource: "carPix", withExtension: nil) {
let fm = FileManager()
return try? fm.contentsOfDirectory(at: f, includingPropertiesForKeys: nil, options: [])
}
return nil