gpt4 book ai didi

ios - Swift 3 上传视频到服务器

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:34 26 4
gpt4 key购买 nike

我正在尝试将视频上传到我的服务器。我目前可以很好地上传图片,但是在尝试上传视频时,我不知道如何处理这个问题。我目前使用以下方式上传图片:

at "let image"当我从相册中选择视频时抛出错误。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{

let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageName!)
let image = info[UIImagePickerControllerOriginalImage]as! UIImage
let data = UIImagePNGRepresentation(image)

最佳答案

在这里,我们使用 Alamofire 库上传视频,请按照以下步骤轻松上传视频。第 1 步:- 添加扩展程序并选择视频

extension UploadStatusViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate
{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

if let mediaType = info[UIImagePickerControllerMediaType] as? String {

if mediaType == "public.movie" {
print("Video Selected")
let videoURL = info[UIImagePickerControllerMediaURL] as! URL

selectedVideoURL = videoURL
self.playVideo(videoURL)
}
}
dismiss(animated: true, completion: nil)

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}

第 2 步:- 添加 Alamofire 库并设置此方法

func callAPIForUploadVideo{

ShowLoaderOnView()
Alamofire.upload(multipartFormData: { (multipartFormData) in
// code
// here you can upload only mp4 video
multipartFormData.append(self.selectedVideoURL!, withName: "File1", fileName: "video.mp4", mimeType: "video/mp4")
// here you can upload any type of video
//multipartFormData.append(self.selectedVideoURL!, withName: "File1")
multipartFormData.append(("VIDEO".data(using: String.Encoding.utf8, allowLossyConversion: false))!, withName: "Type")

}, to: /* Set Url Here */ , encodingCompletion: { (result) in
// code
switch result {
case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
upload.validate().responseJSON {
response in
HideLoaderOnView()
if response.result.isFailure {
debugPrint(response)
} else {
let result = response.value as! NSDictionary
print(result)
}
}
case .failure(let encodingError):
HideLoaderOnView()
NSLog((encodingError as NSError).localizedDescription)
}
})
}

关于ios - Swift 3 上传视频到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43270342/

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