iOS UIImagePickerController: Any way of getting th

2020-02-06 03:47发布

Within my application I am using an UIImagePickerController which calls

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

on success. The thing is, i need the date at which the image was taken. If the user shot a new picture i can take the current date of course, but what can I do in case the user chose a picture from his camera roll or another saved picture?

3条回答
一纸荒年 Trace。
2楼-- · 2020-02-06 04:22

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.

查看更多
【Aperson】
3楼-- · 2020-02-06 04:23
- (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

查看更多
别忘想泡老子
4楼-- · 2020-02-06 04:35

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)
    }
}
查看更多
登录 后发表回答