swift: load UIImage from local path

52 views Asked by At

The app I'm making has the following features.

  • Displays a list of articles in a table view.
  • You can add a post to the post list using the new post function.
  • You can include images when writing a new post.
  • The image path is saved in the db.
  • When importing an image of an article in table view, import it using the path saved at this time.

I am storing data locally via Realm. When writing a new post, the path to the selected image is saved as a string. And when the list of posts is loaded into the tableView, an image is displayed. At this time, the local path saved in the db is used. But for some reason I can't seem to get the image to the local path.

The result of the code below is as follows:

[code]

guard FileManager.default.fileExists(atPath: contentImage) else {
  print(#fileID, #function, #line, " - Error: FileManager file does not exist.")
  return
}
  • 'contentImage' is like: file:///Users/..../Library/Developer/CoreSimulator/Devices/52B35CA1-70BE-4790-8F99-4A93071DD827/data/Containers/Data/Application/CA34093B-7857-48F9-8591-63B7C50B9C5D/Documents3DA97E9F-DBB1-48BB-B30F-546A1CA17D0A.jpeg

[result(error)]

..../PostCell.swift setPost(_:) 129  - Error: FileManager file does not exist.

what can I to do?

additional code is here:

  • Saving local path of an Image
extension CreatePostViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)
        
    if let imgUrl = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.imageURL)] as? URL {
            let imgName = imgUrl.lastPathComponent
            let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
            let localPath = documentDirectory?.appending(imgName)

            let image = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as! UIImage
            let data = image.pngData()! as NSData
            data.write(toFile: localPath!, atomically: true)
            selectImageURL = URL.init(fileURLWithPath: localPath!)
            
            picker.dismiss(animated: true) { () in
                self.imageView.snp.makeConstraints {
                    $0.height.equalTo(self.writingStackView.snp.width)
                }
                
                self.imageView.image = image
            }
        }
    }
  • load image using local path saved in DB
guard FileManager.default.fileExists(atPath: contentImage) else {
    print(#fileID, #function, #line, " - Error: FileManager file does not exist.")
    return
}

    let url = NSURL(string: contentImage)
    let data = NSData(contentsOf: url! as URL)
    self.contentsImage.image = UIImage(data: data! as Data)
0

There are 0 answers