iOS UIImagePickerController:获取所选图片日期的任何方式?

时间:2011-10-07 14:53:50

标签: ios uiimagepickercontroller

在我的应用程序中,我使用的是调用

的UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

成功。 问题是,我需要拍摄图像的日期。如果用户拍摄新照片,我当然可以拍摄当前日期,但如果用户从相机胶卷或其他保存的照片中选择了照片,我该怎么办?

3 个答案:

答案 0 :(得分:4)

您可以将此代码用于从相册中提取的照片和视频。 info 是上述委托方法中的第二个参数。

NSURL *mediaUrl = info[UIImagePickerControllerReferenceURL];
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];

[assetsLibrary assetForURL:mediaUrl resultBlock:^(ALAsset *asset) {
    NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
    // ...
} failureBlock:nil];

同样,要使其工作,您需要包含项目 AssetsLibrary 框架。

答案 1 :(得分:2)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSDictionary *metadataDictionary = (NSDictionary *)[info valueForKey:UIImagePickerControllerMediaMetadata];
        // do something with the metadata

    NSLog(@"meta : %@ \n\n",metadataDictionary);
}

然后

你必须从那个

获取“DateTime”键的值

答案 2 :(得分:2)

iOS 11 +,Swift 4 +

import Photos
extension ViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    public func imagePickerController(_ picker: UIImagePickerController,
                                      didFinishPickingMediaWithInfo info: [String : Any]) {
        if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset,
            let creationDate = asset.creationDate {
            print(creationDate) // Here is the date when your image was taken
        }
        dismiss(animated: true)
    }
}