ALAsset timestamp returns wrong date
I am trying to get the timestamp of images, I can get the correct latitude and longitude values, but the timestamp always returns the current time, not the EXIF time of the image.
ALAssetsLibraryAssetForURLResultBlock resultsBlock = ^(ALAsset *asset) {
CLLocation *imageLoc = [asset valueForProperty:ALAssetPropertyLocation];开发者_如何学Python
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/YY HH:mm:ss"];
NSString *trailTime = [formatter stringFromDate:imageLoc.timestamp];
NSLog(@"---+++ image TimeStamp: %@", trailTime);
[formatter release];
Any help appreciated, thanks
You will need to get the date using ALAssetPropertyDate
key.
NSDate * date = [asset valueForProperty:ALAssetPropertyDate];
/* Use the `NSDateFormatter` instance to print the date */
OK I found the answer What gave me the entire metadata in dictionary format was:
NSDictionary *metadata = asset.defaultRepresentation.metadata;
//Hope this helps others.
It seems like you're fetching location to get the date. you should be doing something like the following:
ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
[assetsLib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
//If you'd want to directly fetch from it's external property, which seems more appropriate.
NSDate *date = [result valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"dd-mm-yyyy hh:mm:ss ZZZ"];
NSString *stringDate = [dateFormatter stringFromDate:date];
//If you'd want to fetch the date from metaData
ALAssetRepresentation *assetRep = [result defaultRepresentation];
NSDictionary *dictionaryOfMetaData = [assetRep metadata];
NSLog(@"dictionary:%@ \n \
date:%@ \n \
StringDate:%@", [[dictionaryOfMetaData valueForKey:@"{TIFF}"] valueForKey:@"DateTime"],
date,
stringDate);
}];
}
failureBlock:^(NSError *error) {
//Handle Error!
}];
精彩评论