如何在Array(或某些数据结构?)中获取Assets.xcassets文件名? [英] How to get Assets.xcassets file names in an Array (or some data structure?)

查看:251
本文介绍了如何在Array(或某些数据结构?)中获取Assets.xcassets文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swift迭代我放入Assets文件夹的图像。我想迭代它们并稍后将它们插入到 .nib 文件中,但到目前为止我找不到如何得到类似的东西:

I'm trying to use Swift to iterate over the images I have put into my Assets folder. I'd like to iterate over them and insert them into a .nib file later, but so far I cannot find how to get something like:

让assetArray = [image1.gif,image2.gif,...]

这可能吗?我一直在玩 NSBundle.mainBundle(),但在这上面找不到任何东西。请告诉我。谢谢!

Is this possible? I've been playing with NSBundle.mainBundle() but couldn't find anything on this. Please let me know. Thanks!

推荐答案

Assets.xcassets不是文件夹,而是包含使用Assets.car作为其文件名的所有图像的存档。

Assets.xcassets is not a folder but an archive containing all the images using Assets.car as its filename.

如果你真的想读取资产文件,那么你需要使用一些可以提取文件内容的库,如下所示 one

If you really want to read the assets file then you need to use some library that can extract the contents of the file like this one.

或者您可以在项目中创建一个包并拖动你在那里的所有图像。就我而言,我的项目中有Images.bundle。要获取文件名,您可以执行以下操作:

Or you can create a bundle in your project and drag all the images you have there. In my case, I have Images.bundle in my project. To get the filenames you can do the following:

let fileManager = NSFileManager.defaultManager()
let bundleURL = NSBundle.mainBundle().bundleURL
let assetURL = bundleURL.URLByAppendingPathComponent("Images.bundle")
let contents = try! fileManager.contentsOfDirectoryAtURL(assetURL, includingPropertiesForKeys: [NSURLNameKey, NSURLIsDirectoryKey], options: .SkipsHiddenFiles)

for item in contents
{
  print(item.lastPathComponent)
}

SWIFT 3版本:

SWIFT 3 Version:

let fileManager = FileManager.default
let bundleURL = Bundle.main.bundleURL
let assetURL = bundleURL.appendingPathComponent("Images.bundle")
let contents = try! fileManager.contentsOfDirectory(at: assetURL, includingPropertiesForKeys: [URLResourceKey.nameKey, URLResourceKey.isDirectoryKey], options: .skipsHiddenFiles)

for item in contents
{
    print(item.lastPathComponent)
}

这篇关于如何在Array(或某些数据结构?)中获取Assets.xcassets文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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