gpt4 book ai didi

ios - 是否可以计算 Assets 目录中具有特定前缀的图片?

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:41 27 4
gpt4 key购买 nike

是否可以计算 Assets 目录中具有特定前缀的图片?例如,我有这样的图片:

鸡尾酒_01

鸡尾酒_02

...

鸡尾酒会

和其他类似的团体。

当我的应用程序启动时,我执行如下代码:

var rec_cocktail = [UIImage]()
for i in 1..<32 {
if i < 10 {
let name: String = "Cocktail__0" + i.description
rec_cocktail.append(UIImage(named: name)!)
} else {
let name: String = "Cocktail__" + i.description
rec_cocktail.append(UIImage(named: name)!)
}
}
alcoholImages.append(rec_cocktail)

它工作完美,但我要加载的组很少,而且每个组都有不同数量的图像。每次从 Assets 目录中添加或删除图片时,我都必须检查和更改范围。

最佳答案

使用文件夹而不是 Images.xcassets 可能更容易。在您的计算机上创建您喜欢的层次结构并将其拖到您的 Xcode 项目中。确保选择:

Create folder references for any added folders

然后因为您现在在构建应用程序时有文件夹引用,您可以使用循环迭代这些文件夹内的项目。

例如,我拖入了一个名为“Cocktail”的文件夹并创建了引用。现在我可以使用以下方法遍历此文件夹中的项目:

let resourcePath = NSURL(string: NSBundle.mainBundle().resourcePath!)?.URLByAppendingPathComponent("Cocktail")
let resourcesContent = try! NSFileManager().contentsOfDirectoryAtURL(resourcePath!, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles)

for url in resourcesContent {
print(url)
print(url.lastPathComponent)
print(url.pathExtension!) // Optional

}

url.lastPathComponent 是图像的文件名(例如 Cocktail_01.jpeg),url 本身是图像的完整路径。

如果您维护文件夹结构,则很容易迭代它们,如果您希望所有图像都在同一个文件夹中,则可以创建一个仅包含所需图像名称的数组,并使用以下方法迭代该数组:

// The Array of Image names
var cocktailImagesArray : [String] = []

// Add images to the array in the 'for url in resourceContent' loop
if (url.lastPathComponent?.containsString("Cocktail")) {
self.cocktailImagesArray.append(url.lastPathComponent)
}

这样您就拥有了包含 Cocktail 的所有图像并将它们添加到您的数组中。现在您可以使用以下方法简单地迭代新创建的数组:

for imageName in self.cocktailImagesArray {
// Do something
}

关于ios - 是否可以计算 Assets 目录中具有特定前缀的图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com