ALAsset时间戳返回错误的日期

时间:2011-07-01 04:33:47

标签: iphone objective-c alasset assetslibrary

我正在尝试获取图像的时间戳,我可以获得正确的纬度和经度值,但时间戳总是返回当前时间,而不是图像的EXIF时间。

ALAssetsLibraryAssetForURLResultBlock resultsBlock = ^(ALAsset *asset) {
    CLLocation *imageLoc = [asset valueForProperty:ALAssetPropertyLocation];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/YY HH:mm:ss"];
    NSString *trailTime = [formatter stringFromDate:imageLoc.timestamp];
    NSLog(@"---+++ image TimeStamp: %@", trailTime);
    [formatter release];

感谢任何帮助,谢谢

3 个答案:

答案 0 :(得分:13)

您需要使用ALAssetPropertyDate键获取日期。

NSDate * date = [asset valueForProperty:ALAssetPropertyDate];
/* Use the `NSDateFormatter` instance to print the date */

答案 1 :(得分:3)

好的,我找到了答案 是什么给了我字典格式的整个元数据:

NSDictionary *metadata = asset.defaultRepresentation.metadata; 

//希望这有助于其他人。

答案 2 :(得分:1)

看起来您正在获取位置以获取日期。你应该做以下事情:


    ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];

    [assetsLib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                             usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                                 [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

                                     //If you'd want to directly fetch from it's external property, which seems more appropriate.
                                     NSDate *date = [result valueForProperty:ALAssetPropertyDate];
                                     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                                     [dateFormatter setLocale:[NSLocale currentLocale]];
                                     [dateFormatter setDateFormat:@"dd-mm-yyyy hh:mm:ss ZZZ"];
                                     NSString *stringDate = [dateFormatter stringFromDate:date];

                                     //If you'd want to fetch the date from metaData
                                     ALAssetRepresentation *assetRep = [result defaultRepresentation];
                                     NSDictionary *dictionaryOfMetaData = [assetRep metadata];

                                     NSLog(@"dictionary:%@ \n \
                                           date:%@ \n \
                                           StringDate:%@", [[dictionaryOfMetaData valueForKey:@"{TIFF}"] valueForKey:@"DateTime"],
                                           date,
                                           stringDate);
                                 }];
                             }
                           failureBlock:^(NSError *error) {
                              //Handle Error!  
                           }];