Get image from documents directory swift

后端 未结 4 634
清酒与你
清酒与你 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

    0 讨论(0)
  • 2020-11-27 14:12

    Better as an extension.

    extension URL {
        static var documentsDirectory: URL {
            let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            return try! documentsDirectory.asURL()
        }
    
        static func urlInDocumentsDirectory(with filename: String) -> URL {
            return documentsDirectory.appendingPathComponent(filename)
        }
    }
    

    Used like this:

    let path = URL.urlInDocumentsDirectory(with: filename).path
    let image = UIImage(contentsOfFile: path)
    
    0 讨论(0)
  • 2020-11-27 14:15

    Swift 2

    If you want to get a file from your document directory in Swift 2:

    let path: String? = NSBundle.mainBundle().pathForResource("imageName", ofType: "png", inDirectory: "DirectoryName/Images")
    let imageFromPath = UIImage(contentsOfFile: path!)!
    self.myImage.image = imageFromPath
    

    Hope that helps somebody

    0 讨论(0)
  • 2020-11-27 14:26

    You are finding the document directory path at runtime for writing the image, for reading it back, you can use the exact logic:

    Swift 3 and Swift 4.2

    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("Image2.png")
       let image    = UIImage(contentsOfFile: imageURL.path)
       // Do whatever you want with the image
    }
    

    Swift 2

    let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
    let nsUserDomainMask    = NSSearchPathDomainMask.UserDomainMask
    if let paths            = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
    {
         if paths.count > 0
         {
             if let dirPath   = paths[0] as? String
             {
                 let readPath = dirPath.stringByAppendingPathComponent("Image2.png")
                 let image    = UIImage(contentsOfFile: readPath)
                 // Do whatever you want with the image
             }
         }
    }
    
    0 讨论(0)
提交回复
热议问题