作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试将视频上传到我的服务器。我目前可以很好地上传图片,但是在尝试上传视频时,我不知道如何处理这个问题。我目前使用以下方式上传图片:
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)
最佳答案
在这里,我们使用 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)
}
})
}
关于ios - Swift 3 上传视频到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43270342/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!