objective-c - ALAssetPropertyDate 返回 "wrong"日期

标签 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] 但它们返回了相同的日期。 任何帮助是极大的赞赏 !

最佳答案

您还可以通过ALAsset获取Exif DateTimeOriginal

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

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

要解决内存不足的问题,您可以使用 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];
}

编辑:更正错误的日期格式(SS -> ss)。感谢@code-roadie

关于objective-c - ALAssetPropertyDate 返回 "wrong"日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6030384/

相关文章:

ios - Realm 线程困惑

objective-c - 合并连接的 UIBezierPaths

iphone - 以编程方式设置 UIDatepicker

ios - Swift - 到达另一个程序来做某事

php - mysql 中 SUM(IF) 条件出现问题

java - 验证日期并指定java中日期的格式

sql-server - 仅工作日的 T-SQL 日期差异

iphone - 返回/传递引用作为其协议(protocol)

iphone - 如何将 NSArray 发送到 Web 服务

iphone - 从 ACAccountStore 在 iOS 中获取 Facebook uid?