无法检索文件,错误:由于您无权查看该文件,因此无法打开该文件

时间:2016-06-08 18:09:14

标签: ios swift swift2

我正在使用的应用程序工作得非常好。我可以从库中选择一张图片并上传到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我检查了编译器是默认的。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

一旦你超出了didFinishPickingMediaWithInfo的范围,我猜你就会被拒绝访问。

也许你可以从中提取图片,而不是保留网址?

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

编辑:

对不起,没有注意到你在Swift,你已经拉了图片。不过,我的建议仍然存在。保持图像而不是URL,你应该没问题。