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

最后都变了- 提交于 2019-11-27 06:23:20

问题


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?


回答1:


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.




回答2:


- (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




回答3:


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)
    }
}


来源:https://stackoverflow.com/questions/7688983/ios-uiimagepickercontroller-any-way-of-getting-the-date-of-the-chosen-picture

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