How to get photo creation date in objective c

2019-09-20 11:37发布

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.

2条回答
神经病院院长
2楼-- · 2019-09-20 12:21

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];

}
查看更多
对你真心纯属浪费
3楼-- · 2019-09-20 12:27

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];

}
查看更多
登录 后发表回答