I am trying to upload a video to my server. I can currently upload images fine but when trying to upload a video I have no clue on how to approach this. I currently use the following for uploading images:
我正在尝试将视频上传到我的服务器。我目前可以上传图片,但在尝试上传视频时,我不知道如何处理这个问题。我目前使用以下内容上传图片:
at "let image" Throws a error when I select a video from the album.
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)
2 个解决方案
#1
2
here , we have upload video using Alamofire library, follow below steps so you can easy uploaded video. Step 1. :- add extension and pick the video
在这里,我们使用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)
}
}
Step 2. :- and add Alamofire library and set this method
第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)
}
})
}
#2
0
You can use Alamofire (https://github.com/Alamofire/Alamofire) for uploading and downloading of file
您可以使用Alamofire(https://github.com/Alamofire/Alamofire)上传和下载文件
Please check below examples of alamofire for uploading file
请查看下面的alamofire上传文件示例
Example 1: Uploading Data
示例1:上传数据
let imageData = UIPNGRepresentation(image)!
Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in
debugPrint(response)
}
Example 2: Uploading a File
示例2:上传文件
let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov")
Alamofire.upload(fileURL, to: "https://httpbin.org/post").responseJSON { response in
debugPrint(response)
}
Example 3: Uploading Multipart Form Data
示例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)
}
}
)
#1
2
here , we have upload video using Alamofire library, follow below steps so you can easy uploaded video. Step 1. :- add extension and pick the video
在这里,我们使用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)
}
}
Step 2. :- and add Alamofire library and set this method
第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)
}
})
}
#2
0
You can use Alamofire (https://github.com/Alamofire/Alamofire) for uploading and downloading of file
您可以使用Alamofire(https://github.com/Alamofire/Alamofire)上传和下载文件
Please check below examples of alamofire for uploading file
请查看下面的alamofire上传文件示例
Example 1: Uploading Data
示例1:上传数据
let imageData = UIPNGRepresentation(image)!
Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in
debugPrint(response)
}
Example 2: Uploading a File
示例2:上传文件
let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov")
Alamofire.upload(fileURL, to: "https://httpbin.org/post").responseJSON { response in
debugPrint(response)
}
Example 3: Uploading Multipart Form Data
示例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)
}
}
)