/
NSMutableDictionary+ImageMetadata.m
175 lines (137 loc) · 6.08 KB
/
NSMutableDictionary+ImageMetadata.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// NSMutableDictionary+ImageMetadata.m
//
// Created by Gustavo Ambrozio on 28/2/11.
//
#import "NSMutableDictionary+ImageMetadata.h"
#import <ImageIO/ImageIO.h>
/* Add this before each category implementation, so we don't have to use -all_load or -force_load
* to load object files from static libraries that only contain categories and no classes.
*
* See http://developer.apple.com/library/mac/#qa/qa2006/qa1490.html for more info.
*/
@interface FIX_CATEGORY_BUG_ImageMetadataCategory @end
@implementation FIX_CATEGORY_BUG_ImageMetadataCategory @end
@implementation NSMutableDictionary (ImageMetadataCategory)
- (id)initWithImageSampleBuffer:(CMSampleBufferRef) imageDataSampleBuffer {
CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
self = [self initWithDictionary:(NSDictionary*)metadataDict];
CFRelease(metadataDict);
return self;
}
// Mostly from here: http://stackoverflow.com/questions/3884060/need-help-in-saving-geotag-info-with-photo-on-ios4-1
- (void)setLocation:(CLLocation *)currentLocation {
if (currentLocation) {
CLLocationDegrees exifLatitude = currentLocation.coordinate.latitude;
CLLocationDegrees exifLongitude = currentLocation.coordinate.longitude;
NSString *latRef;
NSString *lngRef;
if (exifLatitude < 0.0) {
exifLatitude = exifLatitude * -1;
latRef = @"S";
} else {
latRef = @"N";
}
if (exifLongitude < 0.0) {
exifLongitude = exifLongitude * -1;
lngRef = @"W";
} else {
lngRef = @"E";
}
NSDictionary* locDict = [[NSDictionary alloc] initWithObjectsAndKeys:
currentLocation.timestamp, (NSString*)kCGImagePropertyGPSTimeStamp,
latRef, (NSString*)kCGImagePropertyGPSLatitudeRef,
[NSNumber numberWithFloat:exifLatitude], (NSString*)kCGImagePropertyGPSLatitude,
lngRef, (NSString*)kCGImagePropertyGPSLongitudeRef,
[NSNumber numberWithFloat:exifLongitude], (NSString*)kCGImagePropertyGPSLongitude,
nil];
[self setObject:locDict forKey:(NSString*)kCGImagePropertyGPSDictionary];
[locDict release];
}
}
- (NSMutableDictionary *)dictionaryForKey:(CFStringRef)key {
NSDictionary *dict = [self objectForKey:(NSString*)key];
NSMutableDictionary *mutableDict;
if (dict == nil) {
mutableDict = [NSMutableDictionary dictionaryWithCapacity:1];
[self setObject:mutableDict forKey:(NSString*)key];
} else {
if ([dict isMemberOfClass:[NSMutableDictionary class]])
{
mutableDict = (NSMutableDictionary*)dict;
} else {
mutableDict = [[dict mutableCopy] autorelease];
[self setObject:mutableDict forKey:(NSString*)key];
}
}
return mutableDict;
}
#define EXIF_DICT [self dictionaryForKey:kCGImagePropertyExifDictionary]
#define TIFF_DICT [self dictionaryForKey:kCGImagePropertyTIFFDictionary]
#define IPTC_DICT [self dictionaryForKey:kCGImagePropertyIPTCDictionary]
- (void)setUserComment:(NSString*)comment {
[EXIF_DICT setObject:comment forKey:(NSString*)kCGImagePropertyExifUserComment];
}
- (void)setDateOriginal:(NSDate *)date {
[EXIF_DICT setObject:date forKey:(NSString*)kCGImagePropertyExifDateTimeOriginal];
[TIFF_DICT setObject:date forKey:(NSString*)kCGImagePropertyTIFFDateTime];
}
- (void)setDateDigitized:(NSDate *)date {
[EXIF_DICT setObject:date forKey:(NSString*)kCGImagePropertyExifDateTimeDigitized];
}
- (void)setMake:(NSString*)make model:(NSString*)model software:(NSString*)software {
NSMutableDictionary *tiffDict = TIFF_DICT;
[tiffDict setObject:make forKey:(NSString*)kCGImagePropertyTIFFMake];
[tiffDict setObject:model forKey:(NSString*)kCGImagePropertyTIFFModel];
[tiffDict setObject:software forKey:(NSString*)kCGImagePropertyTIFFSoftware];
}
- (void)setDescription:(NSString*)description {
[TIFF_DICT setObject:description forKey:(NSString*)kCGImagePropertyTIFFImageDescription];
}
- (void)setKeywords:(NSString*)keywords {
[IPTC_DICT setObject:keywords forKey:(NSString*)kCGImagePropertyIPTCKeywords];
}
/* The intended display orientation of the image. If present, the value
* of this key is a CFNumberRef with the same value as defined by the
* TIFF and Exif specifications. That is:
* 1 = 0th row is at the top, and 0th column is on the left.
* 2 = 0th row is at the top, and 0th column is on the right.
* 3 = 0th row is at the bottom, and 0th column is on the right.
* 4 = 0th row is at the bottom, and 0th column is on the left.
* 5 = 0th row is on the left, and 0th column is the top.
* 6 = 0th row is on the right, and 0th column is the top.
* 7 = 0th row is on the right, and 0th column is the bottom.
* 8 = 0th row is on the left, and 0th column is the bottom.
* If not present, a value of 1 is assumed. */
// Reference: http://sylvana.net/jpegcrop/exif_orientation.html
- (void)setImageOrientarion:(UIImageOrientation)orientation {
int o = 1;
switch (orientation) {
case UIImageOrientationUp:
o = 1;
break;
case UIImageOrientationDown:
o = 3;
break;
case UIImageOrientationLeft:
o = 8;
break;
case UIImageOrientationRight:
o = 6;
break;
case UIImageOrientationUpMirrored:
o = 2;
break;
case UIImageOrientationDownMirrored:
o = 4;
break;
case UIImageOrientationLeftMirrored:
o = 5;
break;
case UIImageOrientationRightMirrored:
o = 7;
break;
}
[self setObject:[NSNumber numberWithInt:o] forKey:(NSString*)kCGImagePropertyOrientation];
}
@end