简体   繁体   English

迅速:从文档目录加载多个文件

[英]swift: Load multiple files from documents directory

I have folder with many files in documents directory. 我在文档目录中有很多文件的文件夹。 In my folder I have files with different extensions (jpg, png, mp3, zip). 在我的文件夹中,我具有不同扩展名的文件(jpg,png,mp3,zip)。 In my code I have stringArray . 在我的代码中,我有stringArray I want to load all files in someArray and after that add files with .png extension from someArray in stringArray How to do it? 我想加载someArray所有文件, someArraystringArray someArray中添加扩展名为.png文件。

Is it possible to do this? 是否有可能做到这一点? Or I should find another way to load multiple files from documents directory? 还是我应该找到另一种方法来从文档目录加载多个文件?

I find answer for Load multiple images from the folder or directory. - Swift 4 我找到了Load multiple images from the folder or directory. - Swift 4 答案 Load multiple images from the folder or directory. - Swift 4 Load multiple images from the folder or directory. - Swift 4

I tried to use this code from answer : 我试图从答案中使用此代码:

func loadImagesFromAlbum(folderName:String) -> [String]{

        let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
        let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
        let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
        var theItems = [String]()
        if let dirPath          = paths.first
        {
            let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(folderName)

            do {
                theItems = try FileManager.default.contentsOfDirectory(atPath: imageURL.path)
                return theItems
            } catch let error as NSError {
                print(error.localizedDescription)
                return theItems
            }
        }
        return theItems
    }

But I can't get files from theItems . 但是我无法从theItems获取文件。 Because theItems it a string array. 因为theItems是一个字符串数组。 What I do wrong? 我做错了什么? How to get files from loadImagesFromAlbum ? 如何从loadImagesFromAlbum获取文件?

I tried to use this code: 我尝试使用此代码:

images = loadImagesFromAlbum(folderName: "/folder1")

But it is not help me. 但这对我没有帮助。 I get only names. 我只有名字。 But I need to get files. 但是我需要获取文件。

Update 更新

I use this code to load image from documents directory : 我使用此代码从documents directory加载图像:

        let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
        let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
        let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
        if let dirPath          = paths.first
        {
            let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("/folder1/1.png")
            myImageView.image = UIImage(contentsOfFile: imageURL.path)
        }

But I need to load 100 images from /folder1 in var images = [String]() . 但是我需要从/folder1中的var images = [String]()加载100张图像。 Because I use var images = [String]() to show images: contentVC.imageName = images[index] // and etc... 因为我使用var images = [String]()来显示图像: contentVC.imageName = images[index] // and etc...

And the code I use above is not very convenient if I need add 40-100 images. 如果需要添加40-100张图像,我上面使用的代码不是很方便。

Update 1 更新1

I have this code to load images from my project and show it: 我有以下代码可以从我的项目中加载图像并显示它:

class PageViewController: UIViewController {

override func viewDidLoad() {
        super.viewDidLoad()

            for page in 1...pageNumber[indexPage] {
                images.append("page\(page).png")

            }
}

func getContentViewController(withIndex index: Int) -> ContentViewController? {
        if index < images.count{
            let contentVC = self.storyboard?.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
            contentVC.itemIndex = index
            contentVC.imageName = images[index]

            return contentVC
        }

        return nil
    }
}

import UIKit

class ContentViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    var itemIndex: Int = 0
    var imageName: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let currentImage = imageName{
            imageView.image = UIImage(named: currentImage)

        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

But I need to load images from documents directory and show it. 但是我需要从文档目录加载图像并显示它。 How to do it? 怎么做?

If I understand your question correctly you want to load an image for a specific name in a specific folder in the Documents folder. 如果我正确理解了您的问题,则要在“ Documents文件夹中的特定文件夹中加载特定名称的图像。

Please try this, the method takes two parameters, the file name (with extension!) and the folder name and returns an UIImage or nil if the image cannot be found or created. 请尝试此操作,该方法采用两个参数,即文件名(带有扩展名!)和文件夹名,如果找不到或创建图像,则返回UIImagenil

func loadImage(withName name : String, from folderName: String) -> UIImage? {
    do {
        let documentsFolderURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
        let imageURL = documentsFolderURL.appendingPathComponent(folderName).appendingPathComponent(name)
        let data = try Data(contentsOf: imageURL)
        return UIImage(data: data)
    } catch {
        return nil
    }
}

There is no benefit to get all file paths with contentsOfDirectory because you have to load the image data one by one anyway. 使用contentsOfDirectory获取所有文件路径没有任何好处,因为无论如何您都必须一次加载图像数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM