[英]How to get array of UIImage, from folder, in Swift?
I have an ordinary Xcode project like this ...我有一个像这样的普通 Xcode 项目......
notice there's a folder (it is an actual folder - not just a group) named "images".请注意有一个名为“images”的文件夹(它是一个实际的文件夹 - 不仅仅是一个组)。 It contains 25 ".png" images.
它包含 25 个“.png”图像。
All I want to do is make an array of UIimage
with each of those images.我想要做的就是用这些图像中的每一个制作一个
UIimage
数组。
(Or even, an array of the image names or similar, that would be fine - then could load them UIImage(named:
) (或者甚至是图像名称或类似名称的数组,那很好 - 然后可以加载它们
UIImage(named:
)
How do I get at that folder "images"??我如何进入该文件夹“图像”? What about a subfolder "images/cars"?
子文件夹“图像/汽车”怎么样?
I tried something like this but it finds nothing...我试过这样的事情,但它什么也没找到......
override func viewDidLoad()
{
let imageArray = NSBundle.mainBundle().URLsForResourcesWithExtension(
"png", subdirectory: "images")
print("test...")
for n:NSURL in imageArray!
{ print("found ..." ,n) }
}
We we assume the images are in the app's resource bundle.我们假设图像在应用程序的资源包中。 If not you need to make sure that your images directory is listed in the "Copy Bundle Resources" in the "Build Phases" of the target.
如果不是,您需要确保您的图像目录列在目标“构建阶段”的“复制捆绑资源”中。
EDIT This is only going copy the images into the app bundle, if you require the folder to be copied to the app bundle per the code below then please use the follow StackOverflow question to set it up correctly.编辑这只会将图像复制到应用程序包中,如果您需要按照下面的代码将文件夹复制到应用程序包中,请使用以下StackOverflow 问题进行正确设置。
This gives us an array of URL's that we can then use with UIImage(data:) and NSData(contentsOfURL:) to create the image when needed.这为我们提供了一个 URL 数组,然后我们可以使用 UIImage(data:) 和 NSData(contentsOfURL:) 在需要时创建图像。
Get the bundle's resource path and append the image directory then get the contents of the directory.获取包的资源路径并附加图像目录,然后获取目录的内容。
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)
}
}
Please try the following:请尝试以下操作:
You have to register your images to "Copy Bundle Resources".您必须将图像注册到“复制捆绑资源”。
You have to add filter module in main Bundle.您必须在主 Bundle 中添加过滤器模块。 enter image description here
在此处输入图片说明
It is working well on my side.它在我这边运行良好。 Maybe you can change filter from "jpg" format into "png" one.
也许您可以将过滤器从“jpg”格式更改为“png”格式。
I've tested on iOS 10.x later, Swift 3.0 and xcode 8.1 version.我已经在 iOS 10.x 之后的 Swift 3.0 和 xcode 8.1 版本上进行了测试。
I would advise against loading all of your images into an array at once.我建议不要一次将所有图像加载到数组中。 Images tend to be large and it's easy to run out of memory and crash.
图像往往很大,很容易耗尽内存和崩溃。
Unless you absolutely have to have all the images in memory at once it's better to keep an array of paths or URLs and load the images one at a time as needed.除非您绝对必须一次将所有图像保存在内存中,否则最好保留一组路径或 URL 并根据需要一次加载一个图像。
Assuming the folder full of images is in your app bundle, you can use the NSBundle method URLsForResourcesWithExtension:subdirectory:
to get an array of NSURL
s to all the files in your images subdirectory, either with a specific filetype, or ALL files (if you pass nil for the extension.)假设您的应用程序包中包含充满图像的文件夹,您可以使用 NSBundle 方法
URLsForResourcesWithExtension:subdirectory:
将NSURL
数组获取到您的图像子目录中的所有文件,具有特定文件类型或所有文件(如果您为扩展传递 nil。)
Once you have an array of file urls you can map it to an array of paths if needed, and then map that to an array of images.一旦你有一个文件 url 数组,你可以根据需要将它映射到一个路径数组,然后将它映射到一个图像数组。
Swift 4斯威夫特 4
if let path = Bundle.main.resourcePath {
let imagePath = path + "/images"
let url = NSURL(fileURLWithPath: imagePath)
let fileManager = FileManager.default
let properties = [URLResourceKey.localizedNameKey,
URLResourceKey.creationDateKey,
URLResourceKey.localizedTypeDescriptionKey]
do {
let imageURLs = try fileManager.contentsOfDirectory(at: url as URL, includingPropertiesForKeys: properties, options:FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)
print("image URLs: \(imageURLs)")
// Create image from URL
let firstImageURL = imageURLs[0]
let firstImageData = try Data(contentsOf: firstImageURL)
let firstImage = UIImage(data: firstImageData)
// Do something with first image
} catch let error as NSError {
print(error.description)
}
}
You can follow these steps to download them:您可以按照以下步骤下载它们:
Create a new folder in finder and add all images (or folder, ... everything).在finder中创建一个新文件夹并添加所有图像(或文件夹,......一切)。
Change folder name + ".bundle" (for example: YourListImage -> YourListImage.bundle).更改文件夹名称 + “.bundle”(例如:YourListImage -> YourListImage.bundle)。
Add folder to project.将文件夹添加到项目。
Add FileManager extension:添加文件管理器扩展:
extension FileManager { func getListFileNameInBundle(bundlePath: String) -> [String] { let fileManager = FileManager.default let bundleURL = Bundle.main.bundleURL let assetURL = bundleURL.appendingPathComponent(bundlePath) do { let contents = try fileManager.contentsOfDirectory(at: assetURL, includingPropertiesForKeys: [URLResourceKey.nameKey, URLResourceKey.isDirectoryKey], options: .skipsHiddenFiles) return contents.map{$0.lastPathComponent} } catch { return [] } } func getImageInBundle(bundlePath: String) -> UIImage? { let bundleURL = Bundle.main.bundleURL let assetURL = bundleURL.appendingPathComponent(bundlePath) return UIImage.init(contentsOfFile: assetURL.relativePath) } }
Use:使用:
let fm = FileManager.default let listImageName = fm.getListFileNameInBundle(bundlePath: "YourListImage.bundle") for imgName in listImageName { let image = fm.getImageInBundle(bundlePath: "YourListImage.bundle/\\(imgName)") }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.