Search code examples
iosswift3file-managerphasset

i have an issue on the file System on the actual device


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


Solution

  • 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.