iOS UIImagePickerController: Any way of getting the date of the chosen Picture?

本小妞迷上赌 提交于 2019-11-28 11:43:07

You can use this code for photos and videos fetched from albums. info is second parameter in the mentioned delegate method.

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

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

As well, to make it work you need to include to project AssetsLibrary framework.

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

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

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

then

u have to get the "DateTime" key's value from that

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)
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!