我有一个像这样的普通 Xcode 项目......
请注意,有一个名为“images”的文件夹(它是一个实际的文件夹 - 而不仅仅是一个组)。它包含 25 个“.png”图像。
我想做的就是做一个array of UIimage
与每一个图像。
(或者甚至是图像名称或类似名称的数组,那就可以了 - 然后可以加载它们UIImage(named:
)
我如何获得该文件夹“图像”?子文件夹“images/cars”怎么样?
我尝试过类似的事情,但什么也没发现......
override func viewDidLoad()
{
let imageArray = NSBundle.mainBundle().URLsForResourcesWithExtension(
"png", subdirectory: "images")
print("test...")
for n:NSURL in imageArray!
{ print("found ..." ,n) }
}
我们假设图像位于应用程序的资源包中。如果没有,您需要确保您的图像目录列在目标的“构建阶段”的“复制捆绑资源”中。
EDIT这只是将图像复制到应用程序包中,如果您需要根据下面的代码将文件夹复制到应用程序包中,请使用以下命令StackOverflow问题 https://stackoverflow.com/questions/10969555/resource-files-in-subdirectory-are-copied-to-root-directory-of-the-app-bundle正确设置它。
这为我们提供了一个 URL 数组,然后我们可以将其与 UIImage(data:) 和 NSData(contentsOfURL:) 一起使用来在需要时创建图像。
获取包的资源路径并附加图像目录,然后获取该目录的内容。
if let path = NSBundle.mainBundle().resourcePath {
let imagePath = path + "/images"
let url = NSURL(fileURLWithPath: imagePath)
let fileManager = NSFileManager.defaultManager()
let properties = [NSURLLocalizedNameKey,
NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey]
do {
let imageURLs = try fileManager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: properties, options:NSDirectoryEnumerationOptions.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)
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)