3

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

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
RexMac66
  • 111
  • 1
  • 9

3 Answers3

13

You will need to get the date using ALAssetPropertyDate key.

NSDate * date = [asset valueForProperty:ALAssetPropertyDate];
/* Use the `NSDateFormatter` instance to print the date */
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • 2
    Thanks, This returns the date/time the image was saved. So if say an image is edited or taken with a photo app then transfered to the camera roll, the date/time will be wrong – RexMac66 Jul 04 '11 at 22:10
  • OK I found the answer: the ALAssetPropertyLocation timestamp contains a time only no date, surely an omission. What gave me the entire metadata in dictionary format was: NSDictionary *metadata = asset.defaultRepresentation.metadata; Hope this helps others. – RexMac66 Jul 04 '11 at 22:11
  • @RexMac66 You should post that as an answer and accept it, or accept this answer above if it helped solve the issue. This way future readers will know what worked. – chown Nov 16 '11 at 22:49
3

OK I found the answer What gave me the entire metadata in dictionary format was:

NSDictionary *metadata = asset.defaultRepresentation.metadata; 

//Hope this helps others.

RexMac66
  • 111
  • 1
  • 9
1

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