我目前正在做一个项目,我需要在其中读取一些(纬度、经度和日期)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/