当前位置:  开发笔记 > 前端 > 正文

Swift 3将视频上传到服务器

如何解决《Swift3将视频上传到服务器》经验,为你挑选了1个好方法。



1> 小智..:

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

推荐阅读
ifx0448363
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有