I have saved my camera captured photo to Photos and then trying to access it. I am able to successfully save it using PHPhotoLibrary and once saved I get a path like this file:///var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG
. Now I need to load this path inside an UIImageView
I tried the below code
let data = try! Data(contentsOf: URL(fileURLWithPath: "file:///var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG"))
let image = UIImage(data: data)
imageView.image = image
but my app crash saying
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=260 "The file “IMG_0048.JPG” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/file:/var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG, NSUnderlyingError=0x280c545a0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
I tried using kingfisher
library but it does not load the image as well using imageView.kf.setImage(with: URL(fileURLWithPath: imageUrl))
but image is not getting displayed
Another way which I tried was using below code, I am extracting image name from the path and then sending to load function
var documentsUrl: URL {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}
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
}
and then using
imageView.image = load(fileName: "IMG_0048.JPG")
but it does not display the image as well