簡體   English   中英

無法檢索文件,錯誤:由於您無權查看該文件,因此無法打開該文件

[英]Cannot retrieve file, error: The file couldn’t be opened because you don’t have permission to view it

我工作的應用程序一直很好。 我可以從庫中選擇一張圖片並上傳到Firebase。 但是,它突然不允許我從照片庫上傳文件。 從模擬器,沒有問題。 只有在我的手機上,我才會收到這個錯誤。

我收到的錯誤是

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

這很奇怪,因為我一直這樣做,但它今天突然停止工作。

這就是我的代碼的樣子。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    // Clear data if picture is repicked

    imageLocalURL = nil
    imageData = nil

    imageSelected = info[UIImagePickerControllerOriginalImage] as? UIImage

    if info[UIImagePickerControllerReferenceURL] != nil {

        // Image is selected from Photo Library
        referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL

    } else {

        // Image is taken from camera
        imageData = UIImageJPEGRepresentation(imageSelected!, 1.0)

    }

    transportImage.image = imageSelected
    dismissViewControllerAnimated(true, completion: nil)

}


func saveDetails() {

    if imageSelected != nil {

           if referenceURL != nil {

                // Image is from photo library

                let assets = PHAsset.fetchAssetsWithALAssetURLs([referenceURL! as NSURL], options: nil)
                let asset = assets.firstObject
                let checkValidation = NSFileManager.defaultManager()
                asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) in
                    self.imageLocalURL = contentEditingInput?.fullSizeImageURL

                    // I did this to check if there is a local URL and if the file exist, but it returns false
                    print(self.imageLocalURL)
                    print(checkValidation.fileExistsAtPath("\(self.imageLocalURL)"))

                    self.uploadTask = storageRef.child(filePath).putFile(self.imageLocalURL!, metadata: nil) { (metadata, error) in
                        if let error = error {
                            print("Error uploading: \(error.description)")
                            return

                        }
                    }

            } else if imageData != nil {

                let imageLocalData = UIImageJPEGRepresentation(imageSelected!, 0.8)
                let metadata = FIRStorageMetadata()
                metadata.contentType = "image/jpeg"
                uploadTask = storageRef.child(filePath).putData(imageLocalData!, metadata: metadata) { (metadata, error) in
                        if let error = error {
                            print("Error uploading: \(error)")
                            return
                        }

                }

                UIImageWriteToSavedPhotosAlbum(imageSelected!, nil, nil, nil)

            }

如果從相機拍攝圖像,則沒有問題。 如果圖像是從照片庫中拍攝的,我會收到該錯誤。

我四處搜索,我嘗試了Option+Clean構建。 我去了Projects->derived data-> delete我檢查編譯器是默認的。

任何人都可以幫我解決這個問題嗎?

一旦你超出了didFinishPickingMediaWithInfo的范圍,我猜你會被拒絕訪問。

也許你可以從中提取圖像,而不是保留URL?

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

編輯:

對不起,沒有注意到你在Swift,你已經拉了圖像。 不過,我的建議仍然存在。 保持圖像而不是URL,你應該沒問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM