Swift:如何从资产中获取图像名称 [英] Swift: How to get image name from assets

查看:129
本文介绍了Swift:如何从资产中获取图像名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在资产中的文件夹中有500mb的图像。我不知道他们的名字。有没有办法访问它们...将它们存储到Array然后显示?

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?

示例:我有3个图像存储在 Assets / folder1 / folder2
文件夹2包含该3图片.....我需要得到这些名字。

Example: I have 3 images stored in Assets/folder1/folder2 folder 2 contains that 3 images..... I need to get those names.

这就是我需要这个的原因....我已经提供了汽车图像库,图像的名称不合逻辑。

Here is the reason I need this.... Ive been provided with library of car images and that images have illogical names.

推荐答案

在资产目录中存储图像时,无法执行所描述的操作。从资产目录中获取资源取决于您知道其实际名称。

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.

而是将这些文件夹直接存储在应用程序包的顶层。现在你有一个可以获得引用的实际文件夹,可以向FileManager询问文件夹中图像的名称。

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.

示例(Swift 2.2,抱歉):

Example (Swift 2.2, sorry):

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

这篇关于Swift:如何从资产中获取图像名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆