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

时间:2017-07-19 17:17:52

标签: swift swift3 uiimagepickercontroller jsqmessagesviewcontroller

我正在使用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)中已经阅读了很多关于它的内容,但不幸的是,它没有解决方案。请帮我理解如何修复此警告? 整个警告信息:

  

正文文件无法访问:   /var/mobile/Media/PhotoData/Sync/100SYNCD/IMG_0001.JPG错误   Domain = NSCocoaErrorDomain Code = 257"文件“IMG_0001.JPG”无法   因为您没有权限查看它而被打开。"   UserInfo = {NSURL = file:///var/mobile/Media/PhotoData/Sync/100SYNCD/IMG_0001.JPG,NSFilePath = / var / mobile / Media / PhotoData / Sync / 100SYNCD / IMG_0001.JPG,   NSUnderlyingError = 0x17464d4a0 {错误域= NSPOSIXErrorDomain代码= 1   "操作不被允许"}}上传照片时出错:未知错误   发生了,请检查服务器响应。

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

1 个答案:

答案 0 :(得分:1)

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

_sass