【问题标题】:How to get photo creation date in objective c如何在目标 c 中获取照片创建日期
【发布时间】:2013-10-17 08:55:27
【问题描述】:

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

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

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

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

【问题讨论】:

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


【解决方案1】:

你可以用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];

}

【讨论】:

  • 请检查文档,自己尝试一下,如果不起作用,请返回具体问题。
  • 好的,我查过了。我的代码是: NSURL *url=[NSURL URLWithString:fileUrl]; ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; //关键UIImagePickerControllerReferenceURL允许你获取一个ALAsset,然后它允许你获取元数据(例如媒体创建的日期) [lib assetForURL:url resultBlock:^(ALAsset *asset) { NSLog(@"created: % @“, 资产); } failureBlock:^(NSError *error) { NSLog(@"error: %@", error); }];输出为:created: (null)
  • 获取这样的url:NSURL* url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
  • 我不能这样做,因为我正在使用 phonegap.. 我从 phonegap 插件获取 url,如下所示:file://localhost/Users/shivam/Library/Application%20Support/iPhone%20Simulator/6.1/应用程序/3007041C-CEA0-4A8C-884D-91F8268542D4/tmp/cdv_photo_004.png
  • 那我帮不了你了。该网址一定有问题。
【解决方案2】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多