ALAssetPropertyDate returns “wrong” date

。_饼干妹妹 提交于 2019-12-21 03:01:22

问题


I'm currently working on a project in which i need to read some (Latitude, Longitude and date ) EXIF data. The location data seems correct, but the date i'm getting seems to be the "date last modified" date.

{
    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
}

My question is: Am i doing something terribly wrong, or is this expected behavior. I also tried to use the loc.timestamp instead of the [asset valueForProperty:ALAssetPropertyDate] but these returned the same date. Any help is greatly appreciated !


回答1:


Though it's not explicitly documented, I'm guessing that this is the expected behavior. The date refers to when the asset was created and when you're modifying the image, you're probably implicitly creating a new asset. Nothing in the ALAsset documentation suggests that its properties correspond to the image's EXIF data.

To access the EXIF data, you could use the Image I/O framework (available since iOS 4.0), specifically the CGImageSourceCopyProperties function.




回答2:


You can also get Exif DateTimeOriginal through 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"]];

Getting metadata from asset requires to load Exif header on memory (or entire image file?) and those methods above seems to use autorelease pool for the memory spaces. This may cause memory shortage or worse crash if you do a batch process for thousands of images.

To work around for memory shortage, you may use Ad-Hoc autorelease pool.

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];
}

EDIT: correct wrong dateFormat (SS -> ss). Thanks @code-roadie



来源:https://stackoverflow.com/questions/6030384/alassetpropertydate-returns-wrong-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!