IOS UIImagePickerController: Any way to get the date of a selected image?

In my application, I use UIImagePickerController which calls

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

for success. The fact is that I need a date when the image was shot. If the user launched a new image, I can take the current date, of course, but what can I do if the user selects a picture from his camera roll or another saved image?

+3
source share
3 answers

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

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

In addition, to make it work, you need to include AssetsLibrary in the project.

+4
source
 - (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 should get the key value "DateTime" from this

+2
source

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) } } 
+1
source

Source: https://habr.com/ru/post/977785/


All Articles