On Mar 2, 2009, at 8:48 PM, Jushin wrote:

I found that in order to convert NSString format of date to NSDate, we
need timezone.
But, EXIF doesn't support timezone unless GPS tags exist.

Yes, this is an unfortunate fact. Depending on what you need the date for, you may need to let the user choose what timezone the camera is in, and adjust the timestamp by the right amount before using the date in GMT/UTC as it should be.


So, here is what I have come up with:

NSDictionary *metadata = (NSDictionary *)
CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSMutableDictionary *muMetadata = [[metadata mutableCopy] autorelease];
[metadata release];

        
NSMutableDictionary *EXIFDictionary = [[[muMetadata
objectForKey:(NSString *)kCGImagePropertyExifDictionary]
                                                                                
mutableCopy] autorelease];

Maybe you are using these later in your code, but you don't need mutable copies of these things to get the date.


NSString *EXIFDate = [[EXIFDictionary objectForKey:(NSString
*)kCGImagePropertyExifDateTimeDigitized] retain];

I'm not sure why you are retaining this here. You do not need to retain this key to use it like you do below.



NSArray *listItems = [EXIFDate componentsSeparatedByString:@":"];
                
NSString *newFormatedDate = [NSString stringWithFormat:@"%...@-%@-%@:%@: %@ %d",
                                                                         
[listItems objectAtIndex:0], [listItems objectAtIndex:1],
[listItems objectAtIndex:2],
                                                                         
[listItems objectAtIndex:3], [listItems objectAtIndex:4],
                                                                         
(([[NSTimeZone systemTimeZone] secondsFromGMT]/60)/60)];

NSDate *aDate = [[NSDate alloc] initWithString:newFormatedDate];

I still think these are not clever way. If you have better idea,
please share it with me :)

Why not use NSDateFormatter instead? Here's a function similar to one we use, simplified a bit further and using the Digitized timestamp you have in your code:

NSDate* PhotoDateFromEXIF(CFDictionaryRef exifProperties) {
NSString* dateTime = (NSString*)CFDictionaryGetValue(exifProperties, kCGImagePropertyExifDateTimeDigitized);
        NSDate* originalDate = nil;
        if (dateTime) {
NSDateFormatter* exifFormat = [[[NSDateFormatter alloc] init] autorelease];
                [exifFormat setDateFormat:@"yyyy:MM:dd HH:mm:ss"];
                originalDate = [exifFormat dateFromString:dateTime];
        }
        return originalDate;
}

hope this helps,
-natevw


On Mon, Mar 2, 2009 at 10:00 PM, Jushin <coco...@gmail.com> wrote:
I need to get time and date from EXIF of a image.
Following is the code snippet I used:

NSDictionary *metadata = (NSDictionary *)
CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSMutableDictionary *muMetadata = [[metadata mutableCopy] autorelease];
[metadata release];

NSMutableDictionary *EXIFDictionary = [[[muMetadata
objectForKey:(NSString *)kCGImagePropertyExifDictionary]
mutableCopy ] autorelease];

NSString *EXIFDate = [[EXIFDictionary objectForKey:(NSString
*)kCGImagePropertyExifDateTimeDigitized] retain];
NSLog(EXIFDate);

NSDate *aDate = [[NSDate alloc] initWithString: EXIFDate];


Here are my questions:

1. the last line, NSDate *aDate returns null.
I think it is because the format of EXIFDate string that was created
right above isn't correct.
When I did NSLog(EXIFDate), I get followings:
2008:11:06 19:12:51

According to the NSDate documentation, string format of time should be:
2008-11-06 19:12:51

2. the returned date string (EXIFData) doesn't have timezone information. Is it possible to get timezone info as well from the EXIF information?

3. Well... eventually what I want to do is, I want to create "unix
time" format of date from image EXIF using timeIntervalSince1970.
Is there any other better way to get this?

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/nate-lists%40calftrail.com

This email sent to nate-li...@calftrail.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to