0
votes

I'm using JSQMessagesViewController to send photo messages in my chats and I face this problem when I'm trying to upload image from gallery. Here is my code:

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)
    }

I`ve read a lot about it in google ( The file “xxx.mp4” couldn’t be opened because you don’t have permission to view it ) , but unfortunately, there is no solution for it. Please help me to understand how could I fix this warning? Whole warning message:

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. this works good in emulator, I'm getting this warning only at real device.

1
add photo library usage description in your info.plist file .and in the value section add any message you wantSamarth Kejriwal
@Samarth Kejriwal already done, still get this warningcoldembrace
use if let photoReferenceUrl = info[UIImagePickerControllerImageURL] as? URL , because UIImagePickerControllerReferenceURLis deprecated `Samarth Kejriwal
use of unresolved identifier UIImagePickerControllerImageURLcoldembrace
I've changed in into UIImagePickerControllerMediaURL but now I have this error: [Generic] Creating an image format with an unknown type is an errorcoldembrace

1 Answers

1
votes

I've rewrite imagePickerController method, now it works perfect

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
        }
    }