Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0125.PNG” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0125.PNG, NSUnderlyingError=0x1c045d130 Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
I am getting above error while using below code
do{
try filemanager.copyItem(at: sourceURL, to: dirConPath1)
}catch let error as NSError{
print(error.debugDescription)
}
Also I have added:
<key>NSPhotoLibraryUsageDescription</key>
<string>want access of photos</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>photos save description.</string>
into the info.plist
I am using Swift 4.1 and Xcode 9.3.1.
if you want to save image in local directory put this method in your file and just call with your image and image name ...
func saveImageToDocumentLocal(name:String,image:UIImage) {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let random = arc4random()
let fileURL = documentsDirectory.appendingPathComponent("\(name)\(random)")
if let data = UIImageJPEGRepresentation(image, 0.5),
!FileManager.default.fileExists(atPath: fileURL.path) {
do {
try data.write(to: fileURL)
print("file saved")
print(fileURL.path)
} catch {
print("error saving file:", error)
}
}else{
}
}