簡體   English   中英

如何在物鏡c中獲取照片創建日期

[英]How to get photo creation date in objective c

我正在嘗試從iPhone的圖片庫中選擇圖片時獲取圖片拍攝日期。 我正在使用下面的代碼來做同樣的事情。

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

但它顯示的是當前日期和時間。

從照片庫中選擇圖片時,有什么方法可以獲取拍攝日期。

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

}

已棄用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];

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM