问题
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.
回答1:
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];
}
回答2:
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];
}
来源:https://stackoverflow.com/questions/19422177/how-to-get-photo-creation-date-in-objective-c