- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
背景
我有一个应用,提示用户选择媒体源:相机 - 或 - 照片库。
一旦媒体捕获完成(通过相机选项)或选择媒体(通过照片库选项),我准备将所选媒体上传到服务器(转换为 NSData,然后转换为 Base64 编码字符串)。这在大多数情况下都很好用。
问题
对于某些 video 文件(从照片库中选取),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???
}
}
最佳答案
这是我最终所做的,尽管我仍然不明白行为上的差异。
....
else if let assetPath = info[UIImagePickerControllerReferenceURL] as? NSURL {
let assets = PHAsset.fetchAssetsWithALAssetURLs([assetPath], options: nil)
if let firstAsset = assets.firstObject as? PHAsset {
PHCachingImageManager().requestAVAssetForVideo(firstAsset, options: nil, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) in
dispatch_async(dispatch_get_main_queue(), {
if let asset = asset as? AVURLAsset {
self.fileToUpload = NSData(contentsOfURL: asset.URL)!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
// upload now
}
})
})
}
}
关于ios - 当 imagePickerController 只返回 UIImagePickerControllerReferenceURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269095/
我正在尝试获取有关我在 UIImagePicker 中选择的视频的一些数据。 因此,当它进入 UIImagePicker 委托(delegate)方法(如下)时,我知道我需要使用信息字典中的 UIIm
从相机获取图像后调用此方法。 - (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingM
我正在尝试使用以下代码获取我刚刚从相机捕获的图像的名称。但是 [info objectForKey:@"UIImagePickerControllerReferenceURL"] 总是返回 nil。如
我正在保存来自 iPhone 相机的图像并管理委托(delegate)方法: - (void)imagePickerController:(UIImagePickerController *)pick
如何只访问 UIImagePickerControllerReferenceURL 的 MIME ext 部分?这是我在 NSLog -(void)imagePickerController:(UII
我想在下载图像并将图像保存到相册时获取 UIImagePickerControllerOriginalImage 和 UIImagePickerControllerReferenceURL.. 谁能建
背景 我有一个应用,提示用户选择媒体源:相机 - 或 - 照片库。 一旦媒体捕获完成(通过相机选项)或选择媒体(通过照片库选项),我准备将所选媒体上传到服务器(转换为 NSData,然后转换为 Bas
我正在尝试打印 imagePickerController: didFinishPickingMediaWithInfo 函数中的所有图像元数据。当我使用 info.objectForKey(UIIm
是否可以使用 UIImageView 设置图像 UIImagePickerControllerReferenceURL 。传统的模式是使用[信息 objectForKey:@"UIImagePicke
我在 NSDictionary 中有一个 NSURL,如下所示: assets-library://asset/asset.JPG?id=1000000001&ext=JPG 此 NSURL 来自 U
我正在努力弄清楚如何从 UIImagePickerControllerReferenceURL 获取图像(我知道我可以直接从 UIImagePickerControllerOriginalImage
当用户使用 UIImagePickerController 选择图像时,UIImagePickerControllerReferenceURL 会给我以下内容: UIImagePickerContro
我正在使用 UIImagePickerController 来允许用户从图像库中选择图像。然后我想在 sqlite 数据库中开始该文件的位置,以便稍后引用。 我一直在谷歌上搜索如何做到这一点,但我的想
我使用从 UIImagePickerController 获取的信息字典上的 UIImagePickerControllerReferenceURL 键获取照片的资源库 URL。网址如下所示: ass
这个问题在这里已经有了答案: Get NSData from assets-library URL (2 个答案) 关闭 6 年前。 -(void)imagePickerController:(UI
我是一名优秀的程序员,十分优秀!