ALAssetPropertyDate返回“错误”日期

时间:2011-05-17 11:54:49

标签: objective-c xcode date exif

我目前正在开展一个项目,我需要阅读一些(纬度,经度和日期)EXIF数据。位置数据似乎是正确的,但我得到的日期似乎是“最后修改日期”日期。

{
    CLLocation *loc = [asset valueForProperty:ALAssetPropertyLocation];
    NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
    //Returns Last modified date(Picture was taken ... let's say september 
    //last year and it would return the date and time I 'modified' the image).
    NSString *latitude  = [NSString stringWithFormat:@"%g",loc.coordinate.latitude];//Returns correct Latitude
    NSString *longitude = [NSString stringWithFormat:@"%g",loc.coordinate.longitude];//Returns correct Longitude
}

我的问题是:我做的事情是非常错误的,还是这种预期的行为。 我还尝试使用loc.timestamp而不是[asset valueForProperty:ALAssetPropertyDate],但这些返回的日期相同。  任何帮助是极大的赞赏 !

2 个答案:

答案 0 :(得分:5)

您还可以通过DateTimeOriginal获取Exif ALAsset

NSDateFormatter *dateFormatter = [[NSDateFormatter new] autorelease];
dateFormatter.dateFormat = @"y:MM:dd HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:[[[[asset defaultRepresentation] metadata] objectForKey:@"{Exif}"] objectForKey:@"DateTimeOriginal"]];

从资产中获取元数据需要在内存(或整个图像文件?)上加载Exif标头,并且上述方法似乎使用自动释放池作为内存空间。如果对数千张图像进行批处理,这可能会导致内存不足或更严重的崩溃。

要解决内存不足问题,可以使用Ad-Hoc自动释放池。

NSDateFormatter *dateFormatter = [[NSDateFormatter new] autorelease];
dateFormatter.dateFormat = @"y:MM:dd HH:mm:ss";
for (ALAsset *asset in thousandsOfAssets) {
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    NSDate *date = [dateFormatter dateFromString:[[[[asset defaultRepresentation] metadata] objectForKey:@"{Exif}"] objectForKey:@"DateTimeOriginal"]];
    // do something
    [pool release];
}

编辑:更正错误的dateFormat(SS - > ss)。谢谢@ code-roadie

答案 1 :(得分:0)

虽然没有明确记录,但我猜这是预期的行为。日期是指创建资产时以及修改图像时,您可能隐含地创建了新资产。 ALAsset文档中没有任何内容表明其属性与图像的EXIF数据相对应。

要访问EXIF数据,您可以使用Image I / O框架(自iOS 4.0起可用),特别是CGImageSourceCopyProperties函数。

相关问题