如何在Swift中从文件夹中获取UIImage数组?

Fat*_*tie 12 uiimage ios swift

我有一个像这样的普通Xcode项目......

在此输入图像描述

注意有一个名为"images"的文件夹(它是一个实际的文件夹 - 而不仅仅是一个组).它包含25".png"图像.

我想做的就是用这些图像制作一个数组 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) }
    }
Run Code Online (Sandbox Code Playgroud)

Pet*_*sby 6

我们假设图像在应用程序的资源包中.如果不是,您需要确保您的images目录列在目标的"Build Phases"中的"Copy Bundle Resources"中.

在此输入图像描述

编辑 这只是将图像复制到应用程序包中,如果您需要根据下面的代码将文件夹复制到应用程序包,请使用以下StackOverflow问题正确设置它.

这给了我们一个URL数组,然后我们可以使用UIImage(data :)和NSData(contentsOfURL :)来在需要时创建图像.

获取bundle的资源路径并附加image目录,然后获取目录的内容.

     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)
        }
    }
Run Code Online (Sandbox Code Playgroud)


Tên*_*Tên 6

您可以按照以下步骤下载它们:

  1. 在取景器中创建一个新文件夹并添加所有图像(或文件夹,...所有内容)。

  2. 更改文件夹名称+“.bundle”(例如:YourListImage -> YourListImage.bundle)。

  3. 将文件夹添加到项目中。

  4. 添加文件管理器扩展:

    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)
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  5. 使用:

     let fm = FileManager.default
     let listImageName = fm.getListFileNameInBundle(bundlePath: "YourListImage.bundle")
     for imgName in listImageName {
         let image = fm.getImageInBundle(bundlePath: "YourListImage.bundle/\(imgName)")
     }
    
    Run Code Online (Sandbox Code Playgroud)


小智 5

请尝试以下操作:

在此输入图像描述

  1. 您必须将图像注册到“复制捆绑资源”。

  2. 您必须在主捆绑包中添加过滤器模块。 在此输入图像描述

它在我这边运作良好。也许您可以将过滤器从“jpg”格式更改为“png”格式。

我已经在iOS 10.x更高版本、Swift 3.0和xcode 8.1版本上进行了测试。