【发布时间】:2015-10-21 20:57:30
【问题描述】:
背景
我有一个应用程序,提示用户选择媒体源:相机 - 或 - 照片库。
一旦媒体捕获完成(通过相机选项)或选择媒体(通过照片库选项),我准备将所选媒体上传到服务器(转换为 NSData,然后转换为 Base64 编码字符串)。这很好用……在大多数情况下。
问题
对于某些视频文件(从照片库中挑选),info 字典(传递给imagePickerController)不包含UIImagePickerControllerMediaURL 的值。
==> 但是,它确实包含 UIImagePickerControllerReferenceURL 的值。
UIImagePickerControllerReferenceURL 的示例 URL 是:assets-library://asset/asset.MOV?id=39E4A006-F0DB-4EE7-ACB7-624C9B492271&ext=MOV
问题
由于最终目标是转换为 NSData 对象,编码为 Base64 字符串,然后上传,我应该如何处理这个 UIImagePickerControllerReferenceURL 字符串?我已经尝试使用 AssetLibrary 来获取文件,但我在那里遇到了错误。
从概念上讲,是什么导致了这种差异?为什么有些视频文件带有一个键而不是另一个?
代码
let optionMenu = UIAlertController(title: nil, message: "Select Source", preferredStyle: .ActionSheet)
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.mediaTypes = [kUTTypeMovie as String, kUTTypeImage as String]
imagePicker.videoMaximumDuration = 60.0
let cameraAction = UIAlertAction(title: "Camera", style: .Default, handler: {
(alert: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
imagePicker.sourceType = .Camera
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
})
let selectAction = UIAlertAction(title: "Photo Library", style: .Default, handler: {
(alert: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
(alert: UIAlertAction) -> Void in
print("Cancelled", terminator: "")
})
optionMenu.addAction(cameraAction)
optionMenu.addAction(selectAction)
optionMenu.addAction(cancelAction)
self.presentViewController(optionMenu, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
let mediaType = info[UIImagePickerControllerMediaType] as! String
if mediaType == "public.image" {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
// If the image was captured using the camera, save the image to the User's Photo Library
if picker.sourceType == UIImagePickerControllerSourceType.Camera {
UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
// Correct rotation and convert to Base64
self.fileToUpload = image.correctlyOrientedImage().toBase64(UIImage.Quality.Medium)
}
else if mediaType == "public.movie" {
if let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL {
let videoData = NSData(contentsOfURL: videoURL)
if picker.sourceType == UIImagePickerControllerSourceType.Camera {
ALAssetsLibrary().writeVideoAtPathToSavedPhotosAlbum(videoURL, completionBlock: nil)
}
self.fileToUpload = videoData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
}
else if let assetPath = info[UIImagePickerControllerReferenceURL] {
// What do I do here???
}
}
【问题讨论】:
-
将 self.dismissViewControllerAnimated(true, completion: nil) 移动到 didFinishPickingMediaWithInfo 方法的底部