无法打开文件“IMG_0001.JPG”,因为您没有查看它的权限

The file “IMG_0001.JPG” couldn’t be opened because you don’t have permission to view it

我正在使用 JSQMessagesViewController 在聊天中发送照片消息,当我尝试从图库上传图片时遇到了这个问题。这是我的代码:

override func didPressAccessoryButton(_ sender: UIButton) {
        let picker = UIImagePickerController()
        picker.delegate = self
        if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary)) {
            picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        }

        present(picker, animated: true, completion:nil)
    }

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

        picker.dismiss(animated: true, completion:nil)

        // 1
        if let photoReferenceUrl = info[UIImagePickerControllerReferenceURL] as? URL {
            // Handle picking a Photo from the Po8hoto Library
            // 2
            let assets = PHAsset.fetchAssets(withALAssetURLs: [photoReferenceUrl], options: nil)
            let asset = assets.firstObject

            // 3
            if let key = sendPhotoMessage() {
                // 4
                asset?.requestContentEditingInput(with: nil, completionHandler: { (contentEditingInput, info) in
                    let imageFileURL = contentEditingInput?.fullSizeImageURL

                    // 5
                    let path = "\(FIRAuth.auth()?.currentUser?.uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\(photoReferenceUrl.lastPathComponent)"

                    // 6
                    self.storageRef.child(path).putFile(imageFileURL!, metadata: nil) { (metadata, error) in
                        if let error = error {
                            print("Error uploading photo: \(error.localizedDescription)")
                            return
                        }
                        // 7
                        self.setImageURL(self.storageRef.child((metadata?.path)!).description, forPhotoMessageWithKey: key)
                    }
                })
            }
        } else {
            // Handle picking a Photo from the Camera - TODO
        }
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion:nil)
    }

我在 google ( The file “xxx.mp4” couldn’t be opened because you don’t have permission to view it ) 中阅读了很多相关内容,但不幸的是,没有解决方案。请帮助我了解如何解决此警告? 整个警告消息:

Body file is unreachable: /var/mobile/Media/PhotoData/Sync/100SYNCD/IMG_0001.JPG Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0001.JPG” couldn’t be opened because you don’t have permission to view it." UserInfo={NSURL=file:///var/mobile/Media/PhotoData/Sync/100SYNCD/IMG_0001.JPG, NSFilePath=/var/mobile/Media/PhotoData/Sync/100SYNCD/IMG_0001.JPG, NSUnderlyingError=0x17464d4a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} Error uploading photo: An unknown error occurred, please check the server response.

P.S。这在模拟器中运行良好,我只在真实设备上收到此警告。

我重写了 imagePickerController 方法,现在它完美无缺

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

        picker.dismiss(animated: true, completion:nil)

        let stRef = FIRStorage.storage().reference()

        let metaData = FIRStorageMetadata()
        metaData.contentType = "image/jpeg"

        if let photo = info[UIImagePickerControllerOriginalImage] as? UIImage {
            var data = NSData()
            data = UIImageJPEGRepresentation(photo, 0.25)! as NSData
            if let key = sendPhotoMessage() {
                    let path = "\(FIRAuth.auth()?.currentUser?.uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000)))"

                stRef.child(path).put(data as Data, metadata: metaData){ (metaData,error) in
                        if error != nil {
                            return
                        }
                        else {
                            let downloadURL = metaData!.downloadURL()!.absoluteString
                            self.setImageURL(downloadURL, forPhotoMessageWithKey: key)
                        }
                    }
            }
        } else {
            // Handle picking a Photo from the Camera - TODO
        }
    }