Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

how to load image from local path ios swift (by path)

In my app I am storing an image in local storage and I am saving the path of that image in my database. How can I load the image from that path?

Here is the code I am using in order to save the image:

 let myimage : UIImage = UIImage(data: data)!
            let fileManager = NSFileManager.defaultManager()
            let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
            let documentDirectory = urls[0] as NSURL


            print(documentDirectory)
            let currentDate = NSDate()

            let dateFormatter = NSDateFormatter()
            dateFormatter.dateStyle = .NoStyle
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let convertedDate = dateFormatter.stringFromDate(currentDate)
            let imageURL = documentDirectory.URLByAppendingPathComponent(convertedDate)
            imageUrlPath  = imageURL.absoluteString
            print(imageUrlPath)
            UIImageJPEGRepresentation(myimage,1.0)!.writeToFile(imageUrlPath, atomically: true)

And this is the path where my image stored

file:///var/mobile/Containers/Data/Application/B2A1EE50-D800-4BB0-B475-6C7F210C913C/Documents/2016-06-01%2021:49:32

This is how i tried to retrieve the image but it's not displaying anything.

let image : String = person?.valueForKey("image_local_path") as! String
        print(person!.valueForKey("image_local_path")! as! String)
        cell.img_message_music.image = UIImage(contentsOfFile: image)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Folder /B2A1EE50- ... changes every time you run application.

../Application/B2A1EE50-D800-4BB0-B475-6C7F210C913C/Documents/..

Which works for me is to store fileName and get documents folder.

Swift 5

Create getter for directory folder

var documentsUrl: URL {
    return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}

Save image :

private func save(image: UIImage) -> String? {
    let fileName = "FileName"
    let fileURL = documentsUrl.appendingPathComponent(fileName)
    if let imageData = image.jpegData(compressionQuality: 1.0) {
       try? imageData.write(to: fileURL, options: .atomic)
       return fileName // ----> Save fileName
    }
    print("Error saving image")
    return nil
}

Load image :

private func load(fileName: String) -> UIImage? {
    let fileURL = documentsUrl.appendingPathComponent(fileName)
    do {
        let imageData = try Data(contentsOf: fileURL)
        return UIImage(data: imageData)
    } catch {
        print("Error loading image : (error)")
    }
    return nil
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...