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