-
Notifications
You must be signed in to change notification settings - Fork 0
/
JBPhotoTagTools.m
83 lines (63 loc) · 2.97 KB
/
JBPhotoTagTools.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
//
// JBPhotoTagTools.m
// SafetyPic
//
// Created by Jeff Berman on 9/18/14.
// Copyright (c) 2014 Jeff Berman. All rights reserved.
//
// Methods for manipulating metadata tags in photos.
#import "JBPhotoTagTools.h"
#import <ImageIO/ImageIO.h>
@interface JBPhotoTagTools ()
@end
@implementation JBPhotoTagTools
// Return a copy of a photo minus the GPS metadata tags.
+ (NSData *)createCleanedPhotoFromAsset:(ALAsset *)asset
{
// Get the image's current metadata
ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];
NSDictionary *metadata = [assetRepresentation metadata];
NSMutableDictionary *mutableMetadata = [metadata mutableCopy];
// NSLog(@"Metadata: %@", metadata);
// Remove the GPS tags
[mutableMetadata removeObjectForKey:(__bridge NSString *)kCGImagePropertyGPSDictionary];
// Create CGImageRef destination of image + metadata
CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef image = CGImageDestinationCreateWithData(data, (__bridge CFStringRef)[assetRepresentation UTI], 1, NULL);
CGImageDestinationAddImage(image, [assetRepresentation fullResolutionImage], (__bridge CFDictionaryRef)mutableMetadata);
BOOL success = CGImageDestinationFinalize(image);
// Place image + cleaned metadata into an NSData object
NSData *imageData = (__bridge NSData *)data;
CFRelease(data);
CFRelease(image);
return success ? imageData : nil;
}
// Return GPS coordinates from a photo asset. Returns a coordinate
// of (NAN, NAN) if no GPS tags are found.
+ (CLLocationCoordinate2D)gpsCoordinatesFromAsset:(ALAsset *)asset
{
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(NAN, NAN);
if (asset) {
// Get latitude and longitude from photo's metadata
ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];
NSDictionary *metadata = [assetRepresentation metadata];
NSDictionary *gps = metadata[(__bridge NSString *)kCGImagePropertyGPSDictionary];
if (metadata && gps) {
CLLocationDegrees latitude = [gps[(__bridge NSString *)kCGImagePropertyGPSLatitude] doubleValue];
CLLocationDegrees longitude = [gps[(__bridge NSString *)kCGImagePropertyGPSLongitude] doubleValue];
// Set proper latitude value based on hemisphere
NSString *latitudeRef = gps[(__bridge NSString *)kCGImagePropertyGPSLatitudeRef];
if ([latitudeRef isEqualToString:@"S"]) {
latitude = -latitude;
}
// Set proper longitude value based on hemisphere
NSString *longitudeRef = gps[(__bridge NSString *)kCGImagePropertyGPSLongitudeRef];
if ([longitudeRef isEqualToString:@"W"]) {
longitude = -longitude;
}
coordinate = CLLocationCoordinate2DMake(latitude, longitude);
}
}
return coordinate;
}
@end