1
votes

my problem is when i try to copy images from the photo library to the filesystem i get the error the error is

(Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0926.JPG” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0926.JPG, NSUnderlyingError=0x15649630 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}})

my code is :

let fileManager = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let document = fileManager[0]
    var i = 0
    for asset in arrayOfAssets {
        let filePath = document.appendingPathComponent("image\(arrayOfAssets.count + i).png")
        i += 1
        PHImageManager.default().requestImageData(for: asset, options: nil) { (data, nil, _ , info) in
            let url = info?["PHImageFileURLKey"] as! NSURL
            do {
                  try FileManager.default.copyItem(at: url as URL, to: filePath)

            }catch {
                print(error)
            }

           // print(filePath)
        }
    }

please if anyone can help me

thanks all

2
where you get that array of assets ?? arrayOfAssets can you show me this array data fill code ?CodeChanger
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { arrayOfAssets.append(phasset[indexPath.row]) }Haider Ahmed
from didSelectItemInCollectionView and the phase[indexPath.row] i get it from viewdidLoad when i fetch the assetsHaider Ahmed

2 Answers

0
votes

It sounds like you're trying to open a file located here : NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0926.JPG

By default access to this directory is not permitted, you can only access files located in the document directory, for security reasons.

I don't find where this URL comes from in your code, but according to the NSError you are trying to access an image outside the permitted area of the file system.

0
votes

Please check the permission option in plist for Photos Library in iOS 11.

Also you can save data in TEMP directory and fetch it when needed and after use

clean data in TEMP directory.