繁体   English   中英

将视频上传到云存储时出错,但不是图像 (Swift/Xcode/iOS)

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

我正在尝试将视频上传到 Google 的云存储,但遇到错误:

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

这是我的相关 Swift 代码:

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

奇怪的是它在上传图像时起作用。 即,如果我将 kUTypeMovie 更改为 kUTypeImage,以及其他一些使图像正常工作的东西,它就会将其上传到云存储。 当我将其切换回视频时,它失败了。

非常感谢所有帮助 - 如果您需要任何其他信息,请告诉我!

干杯,乔什

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

请让我知道它是否适合您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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