gpt4 book ai didi

ios - 使用 URL swift Alamofire 上传视频

转载 作者:可可西里 更新时间:2023-11-01 01:09:05 24 4
gpt4 key购买 nike

我需要使用 alamofire 将视频上传到服务器。用户选择视频,我在 didFinishPickingMediaWithInfo 中成功获取 URL,如下所示:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil)
if let pickedVideo = info[UIImagePickerControllerMediaURL] as? URL {
print(pickedVideo)
}
}

然后我使用以下代码上传视频:

Alamofire.upload( multipartFormData: { multipartFormData in
multipartFormData.append(videoUrl, withName: "video", fileName: "video.mp4", mimeType: "video/mp4")

}, to: url, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if let JSON = response.result.value as? NSDictionary {
completion(true)
} else {
completion(false)
print(response)
}
}
case .failure(let encodingError):
print(encodingError)
completion(false)
}
})

进入失败 block ,显示如下错误:

multipartEncodingFailed(Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL: file:/private/var/mobile/Containers/Data/Application/C662AB0E-6A4F-40FB-9949-7F0A5AA2BA49/tmp/52C86F07-5DCC-413A-9F8C-71BBF33F793C.MOV -- file:///, error: Error Domain=NSCocoaErrorDomain Code=260 "The file “52C86F07-5DCC-413A-9F8C-71BBF33F793C.MOV” couldn’t be opened because there is no such file.

最佳答案

您正在尝试通过 URL 上传视频,这是不可能的,在 multipartFormData 中需要上传数据而不是 URL,因此首先将其转换为 Data 然后上传。

仅用于视频的显示 imagePickerController 的功能:

func showImagePicker(){
let picker = UIImagePickerController()
picker.delegate = self
picker.mediaTypes = [kUTTypeMovie as String]
self.present(picker, animated: true, completion: nil)
}

UIImagePickerControllerDelegate 函数,在选择视频后起作用:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil)

guard let videoUrl = info[UIImagePickerControllerMediaURL] as? URL else {
return
}
do {
let data = try Data(contentsOf: videoUrl, options: .mappedIfSafe)
print(data)
// here you can see data bytes of selected video, this data object is upload to server by multipartFormData upload
} catch {
}
}

关于ios - 使用 URL swift Alamofire 上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49649315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com