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

后端 未结 8 1629
长情又很酷
长情又很酷 2020-12-04 20:02

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 a

相关标签:
8条回答
  • 2020-12-04 20:30

    Replace absoluteString with path

    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.path
            print(imageUrlPath)
            UIImageJPEGRepresentation(myimage,1.0)!.writeToFile(imageUrlPath, atomically: true)
    
    0 讨论(0)
  • 2020-12-04 20:40

    This code works for me

    func getImageFromDir(_ imageName: String) -> UIImage? {
    
        if let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            let fileURL = documentsUrl.appendingPathComponent(imageName)
            do {
                let imageData = try Data(contentsOf: fileURL)
                return UIImage(data: imageData)
            } catch {
                print("Not able to load image")
            }
        }
        return nil
    }
    
    0 讨论(0)
  • 2020-12-04 20:42

    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 3 +

    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 = UIImageJPEGRepresentation(image, 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
    }
    
    0 讨论(0)
  • 2020-12-04 20:49

    1.cell.image.sd_setShowActivityIndicatorView(true)

    2.cell.image.sd_setIndicatorStyle(.gray)

    3.cell.image.image = UIImage(contentsOfFile: urlString!)

    0 讨论(0)
  • 2020-12-04 20:53

    Swift 4:

    if FileManager.default.fileExists(atPath: imageUrlPath) {
                let url = NSURL(string: imageUrlPath)
                let data = NSData(contentsOf: url! as URL)
    
                chapterImage.image = UIImage(data: data! as Data)
            }
    
    0 讨论(0)
  • 2020-12-04 20:54

    Also you can try this.

    1. Check if your path exist

    if NSFileManager.defaultManager().fileExistsAtPath(imageUrlPath) {}

    1. Create an URL to your path

    let url = NSURL(string: imageUrlPath)

    1. Create data to you URL

    let data = NSData(contentsOfURL: url!)

    1. Bind the url to your imageView

    imageView.image = UIImage(data: data!)

    Final code:

    if NSFileManager.defaultManager().fileExistsAtPath(imageUrlPath) {
        let url = NSURL(string: imageUrlPath)
        let data = NSData(contentsOfURL: url!)
        imageView.image = UIImage(data: data!)
    }
    
    0 讨论(0)
提交回复
热议问题