I am trying to get picture taken date while choosing picture from Photo Library in iPhone. I am using below code to do the same.
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSDate *result = [fileAttribs fileCreationDate]; //or fileModificationDate
But it is showing current date and time.
Is there any way to get picture taken date while choosing picture from Photo library.
You can do it checking the metadata of the picture with ALAsset
Look for the key ALAssetPropertyDate
EDIT, complete code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:url
resultBlock:^(ALAsset *asset) {
NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
NSLog(@"Date: %@", myDate);
} failureBlock:^(NSError *error) {
NSLog(@"Error");
}];
[picker dismissViewControllerAnimated:YES completion:nil];
}
ALAssetsLibrary is deprecated, so with PHAsset:
#import <Photos/PHAsset.h>
Then:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
PHAsset *phAsset = [[PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil] lastObject];
NSDate *date = [phAsset creationDate];
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
}