如果您提供的源代码类型为UIImagePickerController
(Swift)的.Camera
,您如何获取图像的日期(或开始录制,如视频的情况)或UIImagePickerControllerDelegate
方法中的视频?
答案 0 :(得分:2)
您必须访问info
并提取元数据并从中提取日期:
- (void)imagePickerController:(nonnull UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info {
NSString* meta = [[[info objectForKey:UIImagePickerControllerMediaMetadata] objectForKey:@"{TIFF}"] objectForKey:@"DateTime"];
NSDateFormatter *form = [[NSDateFormatter alloc] init];
form.dateFormat = @"yyyy:MM:dd HH:mm:ss";
NSDate* date = [form dateFromString:meta];
[picker dismissViewControllerAnimated:true completion:^{}];
}
答案 1 :(得分:0)
使用imagePickerController(_picker, didFinishPickingMediaWithInfo info)
协议的UIImagePickerControllerDelegate
方法。
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let metaDataDict = info.valueForKey(UIImagePickerControllerMediaMetadata) as? NSDictionary
// print metaDataDict to see it's keys and values
println("meta data = \(metaDataDict.description)")
}
然后,您可以确定照片拍摄日期的关键(请注意[UIImagePickerControllerMediaMetadata][1]
仅适用于照片)。
答案 2 :(得分:0)
首先,您出示UIImagePickerController
,当您收到回调
imagePickerController(_:didFinishPickingMediaWithInfo:)
时,抓取信息词典并提取日期信息:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let url = info[UIImagePickerControllerMediaURL] as? NSURL { // Its possible that there will not be a date present
let assetsLibrary = ALAssetsLibrary()
assetsLibrary.assetForURL(url, resultBlock: { (asset: ALAsset!) -> Void in
if let date = asset.valueForProperty(ALAssetPropertyDate) as? NSDate {
print("\(date)\n")
}
}) { (error: NSError!) -> Void in
// Handle error
}
}
}
请务必与AssetsLibrary
链接。
演示可以这样完成:
let picker = UIImagePickerController()
picker.sourceType = .PhotoLibrary
picker.delegate = self
self.presentViewController(picker, animated: true, completion: nil)