Get image from documents directory swift

后端 未结 4 665
清酒与你
清酒与你 2020-11-27 13:43

Say I were using this code to save an image to the documents directroy

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask         


        
4条回答
  •  孤街浪徒
    2020-11-27 14:06

    Load multiple images from the folder or directory. - Swift 4

    Here's the image attached to show, what we want to achieve in the given below code.

    Here's the code to find the multiple images from the folder in documents directory. I have written one method to do the same.

    In the code we are passing the "Folder Name" (ie. Red) and getting the contents of that directory. In return we got the array of images name.

        static 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
    }
    

    Here's the result of given code.

    Hope it helps.

    Thanks

提交回复
热议问题