ALAssetPropertyDate returns “wrong” date

后端 未结 2 1454
梦谈多话
梦谈多话 2021-02-10 16:25

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 b

2条回答
  •  [愿得一人]
    2021-02-10 16:49

    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

提交回复
热议问题