Swift 3将视频上传到服务器

时间:2017-04-07 05:30:34

标签: ios swift uiimagepickercontroller

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

at"让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)

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

您可以使用Alamofire(https://github.com/Alamofire/Alamofire)上传和下载文件

请查看以下 alamofire 上传文件

的示例

示例1 :上传数据

let imageData = UIPNGRepresentation(image)!

Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in
    debugPrint(response)
}

示例2 :上传文件

let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov")

Alamofire.upload(fileURL, to: "https://httpbin.org/post").responseJSON { response in
    debugPrint(response)
}

示例3:上传多部分表单数据

Alamofire.upload(
    multipartFormData: { multipartFormData in
        multipartFormData.append(unicornImageURL, withName: "unicorn")
        multipartFormData.append(rainbowImageURL, withName: "rainbow")
    },
    to: "https://httpbin.org/post",
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    }
)