在这里,我们已经使用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) } }) }