如何从UIImagePickerController拍摄的照片和视频中获取日期?

时间:2015-07-15 17:20:31

标签: ios objective-c swift

如果您提供的源代码类型为UIImagePickerController(Swift)的.Camera,您如何获取图像的日期(或开始录制,如视频的情况)或UIImagePickerControllerDelegate方法中的视频?

3 个答案:

答案 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)