我正在开发一个应用程序,可以录制电影并将其保存到iPhone的库中,现在我还需要给予将其上传到私人服务器的选项。我正在使用Alamofire库,但我的服务器使用SCP作为上传协议,但我只能找到Alamofire库的http示例。有没有人举个例子或者可以帮助我?谢谢
1bqhqjot1#
我相信你对UIImagePicker有所了解。UIImagePicker的这个委托方法将给出视频路径,并使用该路径在alamofire请求中进行视频上载。
UIImagePicker
alamofire
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { let video_file = info[UIImagePickerController.InfoKey.mediaURL] as? URL if let selectedVideo : URL = (info[UIImagePickerController.InfoKey.mediaURL] as? URL) { let selectorToCall = #selector(videoSaved(_:didFinishSavingWithError:context:)) UISaveVideoAtPathToSavedPhotosAlbum(selectedVideo.relativePath, self, selectorToCall, nil) let videoData = try? Data(contentsOf: selectedVideo) let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] let video_url = documentsDirectory.appendingFormat("/video.mp4") // use this variable in alamofire let url = URL(fileURLWithPath: video_url ?? "") try! videoData?.write(to: url, options: []) } } // this method will save your video in photo library @objc func videoSaved(_ video: String, didFinishSavingWithError error: NSError!, context: UnsafeMutableRawPointer) { if let theError = error { print("error saving the video = \(theError)") } else { DispatchQueue.main.async(execute: { () -> Void in }) } }
使用alamofire上传视频
func uploadImageVideoWithData(apiurl:String , param : [String : Any], videoUrl : URL ) { let headers: HTTPHeaders = ["Content-type": "multipart/form-data","Accept": "application/json"] Alamofire.upload(multipartFormData: { multipartFormData in multipartFormData.append(videoUrl, withName: "key_name", fileName: "video_name.mp4", mimeType: "mp4") for (key, value) in param { multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key) } }, usingThreshold: 10 * 1024 * 1024,to: apiurl, method: .post, headers: headers, encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON { response in print(response.result) if let json = response.result.value { print("JSON: \(json)") } } case .failure(let encodingError): print(encodingError) } }) }
1条答案
按热度按时间1bqhqjot1#
我相信你对
UIImagePicker
有所了解。UIImagePicker
的这个委托方法将给出视频路径,并使用该路径在alamofire
请求中进行视频上载。使用alamofire上传视频