-
Notifications
You must be signed in to change notification settings - Fork 4
/
NSDictionary+ImageMetadata.m
123 lines (101 loc) · 4 KB
/
NSDictionary+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
//
// NSMutableDictionary+ImageMetadata.m
// NSDictionary-ImageMetadata
//
// Created by Steven Grace on 4/22/13.
// Copyright (c) 2013 Steven Grace. All rights reserved.
//
#import "NSDictionary+ImageMetadata.h"
@implementation NSDictionary (ImageMetadata)
#pragma mark Metadata Fetching
+ (NSDictionary*)imageMetadataWithImageAtURL:(NSURL*)url
{
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
if (!imageSource) {
NSLog(@"%s failed to create image at url: %@",__PRETTY_FUNCTION__,url);
return nil;
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
nil];
CFDictionaryRef imageProperties =CGImageSourceCopyPropertiesAtIndex(imageSource,0,(__bridge CFDictionaryRef)options);
CFRelease(imageSource);
NSMutableDictionary* metadata = [NSMutableDictionary dictionaryWithDictionary:(__bridge NSDictionary*)imageProperties];
CFRelease(imageProperties);
return metadata;
}
#pragma mark - Public Property Fetching
- (id)sgg_valueForProperty:(NSString *)property
{
return [self sgg_recursiveObjectForKey:property];
}
#pragma mark - Formatted Property Helpers
- (NSString*)sgg_formattedShutterSpeed
{
NSNumber* time = [self sgg_recursiveObjectForKey:(id)kCGImagePropertyExifShutterSpeedValue];
float d=1/[time floatValue];
return [NSString stringWithFormat:@"1/%g",ceilf(d)];
}
- (NSString*)sgg_formattedApetureValue
{
return [NSString stringWithFormat:@"f/%@",[self sgg_recursiveObjectForKey:(id)kCGImagePropertyExifApertureValue]];
}
#pragma mark - Location
- (CLLocation*)sgg_location
{
NSDictionary *GPSdictionary = [self objectForKey:(id)kCGImagePropertyGPSDictionary];
if (GPSdictionary) {
CLLocationDegrees latituteDegress = [[GPSdictionary objectForKey:(id)kCGImagePropertyGPSLatitude] floatValue];
CLLocationDegrees longitudeDegress = [[GPSdictionary objectForKey:(id)kCGImagePropertyGPSLongitude] floatValue];
NSString *latitudeRef = [GPSdictionary objectForKey:(id)kCGImagePropertyGPSLatitudeRef];
NSString *longtiudeRef = [GPSdictionary objectForKey:(id)kCGImagePropertyGPSLongitudeRef];
if ([@"S" isEqualToString:latitudeRef]) {
latituteDegress *= -1.0f;
}
if ([@"W" isEqualToString:longtiudeRef]) {
longitudeDegress *= -1.0f;
}
CLLocation *location = [[CLLocation alloc] initWithLatitude:latituteDegress longitude:longitudeDegress];
return location;
}
return nil;
}
- (CLLocationDirection)sgg_trueHeading
{
NSDictionary *GPSinfo = [self objectForKey:(id)kCGImagePropertyGPSDictionary];
CLLocationDirection heading = 0;
if (GPSinfo) {
heading =[[GPSinfo objectForKey:(id)kCGImagePropertyGPSImgDirection] doubleValue];
}
return heading;
}
#pragma mark- NSDictionary Recursive
- (id)sgg_recursiveObjectForKey:(NSString *)key
{
if ([self.allKeys containsObject:key]) {
return [self objectForKey:key];
}
for (NSString *k in self.allKeys)
{
id obj = [self objectForKey:k];
if([obj isKindOfClass:[NSDictionary class]])
{
NSDictionary *d = (NSDictionary *)obj;
id child = [d sgg_recursiveObjectForKey:key];
if(child) return child;
} else if([obj isKindOfClass:[NSArray class]]) {
// loop through the NSArray and traverse any dictionaries found
NSArray *a = (NSArray *)obj;
for(id child in a) {
if([child isKindOfClass:[NSDictionary class]]) {
NSDictionary *d = (NSDictionary *)child;
id o = [d sgg_recursiveObjectForKey:key];
if(o) return o;
}
}
}
}
// the key was not found in this dictionary or any of it's children
return nil;
}
@end