-1

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

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

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

从照片库中选择图片时,有什么方法可以获取照片的拍摄日期。

4

2 回答 2

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

}
于 2013-10-17T09:01:29.853 回答
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];

}
于 2018-03-21T12:48:08.573 回答