[英]How to get date from photos AND videos taken from UIImagePickerController?
如果呈现的源图像类型为.Camera
(Swift)的UIImagePickerController
,如何在UIImagePickerControllerDelegate
方法中获取图像或视频的拍摄日期(或开始录制,对于视频而言)。
You have to access the info
and extract the meta data and extract the date from that: 您必须访问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:^{}];
}
Use the imagePickerController(_picker, didFinishPickingMediaWithInfo info)
method of the UIImagePickerControllerDelegate
protocol. 使用UIImagePickerControllerDelegate
协议的imagePickerController(_picker, didFinishPickingMediaWithInfo info)
方法。
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)")
}
You can then determine the key for the date the photo was taken (note that [UIImagePickerControllerMediaMetadata][1]
is only for photos). 然后,您可以确定照片拍摄日期的密钥(请注意[UIImagePickerControllerMediaMetadata][1]
仅用于照片)。
First you present you UIImagePickerController
and when you get the callback imagePickerController(_:didFinishPickingMediaWithInfo:)
grab the info dictionary and extract the date info: 首先,向您展示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
}
}
}
Be sure to link with AssetsLibrary
. 确保与AssetsLibrary
链接。
Presenting can be done like so: 演示可以这样进行:
let picker = UIImagePickerController()
picker.sourceType = .PhotoLibrary
picker.delegate = self
self.presentViewController(picker, animated: true, completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.