简体   繁体   中英

Error uploading a video to Cloud Storage, but not an image (Swift/Xcode/iOS)

I'm trying to upload a video to Google's Cloud Storage, but hit an error:

BackgroundSession <1EE0DA45-0AA5-45FB-AAB4-1580A53F88A8> error requesting a NSURLSessionUploadTask from background transfer daemon: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service on pid 27172 named com.apple.nsurlsessiond was interrupted, but the message was sent over an additional proxy and therefore this proxy has become invalid."
    _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundUploadTask <26C123A0-427F-45B9-B7A3-64AEBD19A2AA>.<1>, NSLocalizedDescription=unknown error}
Optional("An unknown error occurred, please check the server response.")
2021-03-01 17:00:28.536123+0000 ForfeitV3.2[72747:1363373] BackgroundSession <1EE0DA45-0AA5-45FB-AAB4-1580A53F88A8> connection to background transfer daemon invalidated

Here's my relevant Swift code:

    func timelapsePopup() {
    let imagePC = UIImagePickerController()
    imagePC.sourceType = .photoLibrary
    imagePC.delegate = self
    imagePC.sourceType = .savedPhotosAlbum
    imagePC.mediaTypes = [String(kUTTypeMovie)]
    imagePC.allowsEditing = false
    V.timelapse = true
    present(imagePC, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    if V.timelapse == true { //TIMELAPSE
        V.timelapse = false
        if let selectedVideo = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
//                fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
                print("Here's the file URL: ", selectedVideo)
                dismiss(animated: true, completion: nil)
                brain.submitEvidence(itemIndex: V.indexToBePassed, image: UIImage(), timelapsePath: selectedVideo)
            }
            myTableView.reloadData()

        
        
        
    }

    mutating func submitEvidence(itemIndex: Int, image: UIImage, timelapsePath: URL) {
    print(timelapsePath.absoluteString)
    if timelapsePath.absoluteString != "" {
        let item = V.items[itemIndex]
        item.timelapseURL = timelapsePath
        
        item.timeSubmitted = getCurrentTime()
        item.sentForConfirmation = true
        
        self.saveItem(item: item)
        self.addTimelapseToFirestore(item: item)
    } else {
        let item = V.items[itemIndex]
        item.image = image.toString()!
        
        item.timeSubmitted = getCurrentTime()
        item.sentForConfirmation = true
        
        self.saveItem(item: item)
        self.addToFirestore(item: item)
    }
}
    func addTimelapseToFirestore(item: Item) {
    let storage = Storage.storage()
    let data = Data()
    let storageRef = storage.reference()
    let localFile = item.timelapseURL
    let photoRef = storageRef.child("videoOne")
    let uploadTask = photoRef.putFile(from: localFile!, metadata: nil) { (metadata, error) in
        guard let metadata = metadata else {
            print(error?.localizedDescription)
            return
        }
        print("video uploaded")
    }
}

The weird thing is that it works when uploading an image. Ie if I change the kUTypeMovie to kUTypeImage, and a few other things to make images work, it uploads that just fine to Cloud Storage. When i switch it back to video, it fails.

All help greatly appreciated - let me know if there's any other info you need!

Cheers, Josh

Please use the following code which uses UIImagePicker object class to select a video or image in your device and upload it to Firebase ( I have made some changes in your code):

@IBAction func uploadButton(_ sender: Any) {
    // Configuration
    let picker = UIImagePickerController()
    picker.allowsEditing = true
    picker.delegate = self
    picker.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]

    // Present the UIImagePicker Controller
    present(picker, animated: true, completion: nil)
}

// The didFinishPickingMediaWithInfo let's you select an image/video and let's you decide what to do with it. 
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
    if V.timelapse == true { //TIMELAPSE
        V.timelapse = false
        if let selectedVideo = info[UIImagePickerControllerMediaURL] as? NSURL {
//                fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
                print("Here's the file URL: ", selectedVideo)
                dismiss(animated: true, completion: nil)
                brain.submitEvidence(itemIndex: V.indexToBePassed, image: UIImage(), timelapsePath: selectedVideo)
            }
            myTableView.reloadData()

        
        
        
    }

    mutating func submitEvidence(itemIndex: Int, image: UIImage, timelapsePath: URL) {
    print(timelapsePath.absoluteString)
    if timelapsePath.absoluteString != "" {
        let item = V.items[itemIndex]
        item.timelapseURL = timelapsePath
        
        item.timeSubmitted = getCurrentTime()
        item.sentForConfirmation = true
        
        self.saveItem(item: item)
        self.addTimelapseToFirestore(item: item)
    } else {
        let item = V.items[itemIndex]
        item.image = image.toString()!
        
        item.timeSubmitted = getCurrentTime()
        item.sentForConfirmation = true
        
        self.saveItem(item: item)
        self.addToFirestore(item: item)
    }
}
    func addTimelapseToFirestore(item: Item) {
    let storage = Storage.storage()
    let data = Data()
    let storageRef = storage.reference()
    let localFile = item.timelapseURL
    let photoRef = storageRef.child("videoOne.mov")
    let uploadTask = photoRef.putFile(from: localFile!, metadata: nil) { (metadata, error) in
        guard let metadata = metadata else {
            print(error?.localizedDescription)
            return
        }
        print("video uploaded")
    }
}

Please let me know if it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM