如何在目标c中获取照片创建日期

时间:2013-10-17 08:55:28

标签: iphone objective-c date metadata photo-gallery

我正在尝试从iPhone中的照片库中选择照片时拍照。我使用下面的代码来做同样的事情。

NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSDate *result = [fileAttribs fileCreationDate]; //or fileModificationDate

但它显示当前的日期和时间。

从照片库中选择照片时,有没有办法拍摄照片。

2 个答案:

答案 0 :(得分:3)

您可以使用ALAsset

检查图片的元数据

查找密钥ALAssetPropertyDate

编辑,完整代码:

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

}

答案 1 :(得分:0)

不推荐使用ALAssetsLibrary,因此使用PHAsset:

#import <Photos/PHAsset.h> 

然后:

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

}