-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTLFileSourceItem.m
More file actions
152 lines (126 loc) · 4.5 KB
/
TLFileSourceItem.m
File metadata and controls
152 lines (126 loc) · 4.5 KB
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
//
// TLFileSourceItem.m
// Geotagalog
//
// Created by Nathan Vander Wilt on 6/5/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLFileSourceItem.h"
#import "TLFilePhotoSource.h"
#import "TLCocoaToolbag.h"
#import "TLTimestamp.h"
#import "NSFileManager+TLExtensions.h"
#import "NSDateFormatter+TLAdditions.h"
@interface TLFileSourceItem ()
@property (nonatomic, copy) NSDictionary* metadata;
@property (nonatomic, readonly) CGImageSourceRef imageSource;
- (void)updateWithProperties:(NSDictionary*)theProperties;
@end
@implementation TLFileSourceItem
@synthesize originalURL;
@synthesize originalUTI;
@synthesize metadata;
@synthesize imageSource;
- (id)initWithSource:(TLFilePhotoSource*)source
originalURL:(NSURL*)theFileURL
error:(NSError**)err
{
self = [super initWithSource:source];
if (self) {
originalURL = [theFileURL copy];
originalUTI = [TLFileGetUTI(theFileURL) copy];
if (UTTypeConformsTo((CFStringRef)originalUTI, kUTTypeImage)) {
NSDictionary* sourceOptions = [NSDictionary dictionaryWithObjectsAndKeys:
originalUTI, (id)kCGImageSourceTypeIdentifierHint, nil];
imageSource = CGImageSourceCreateWithURL((CFURLRef)theFileURL,
(CFDictionaryRef)sourceOptions);
}
if (imageSource && !CGImageSourceGetCount(imageSource)) {
[self dealloc];
if (err) {
NSDictionary* errInfo = [NSDictionary dictionaryWithObjectsAndKeys:
theFileURL, NSURLErrorKey, nil];
*err = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadCorruptFileError userInfo:errInfo];
}
return nil;
}
if (imageSource) {
CFDictionaryRef imgProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
if (imgProperties) {
[self updateWithProperties:(NSDictionary*)imgProperties];
CFRelease(imgProperties);
}
}
}
return self;
}
- (void)dealloc {
[originalURL release];
[originalUTI release];
[metadata release];
if (imageSource) CFRelease(imageSource);
[super dealloc];
}
- (NSString*)originalFilename {
return [[[self originalURL] path] lastPathComponent];
}
- (void)updateWithProperties:(NSDictionary*)theProperties {
//NSLog(@"%@", theProperties);
NSMutableDictionary* theMetadata = [[[self metadata] mutableCopy] autorelease];
if (!theMetadata) {
theMetadata = [NSMutableDictionary dictionary];
}
NSString* timestampString = [[theProperties objectForKey:(id)kCGImagePropertyExifDictionary]
objectForKey:(id)kCGImagePropertyExifDateTimeOriginal];
if (!timestampString) {
timestampString = [[theProperties objectForKey:(id)kCGImagePropertyExifDictionary]
objectForKey:(id)kCGImagePropertyExifDateTimeDigitized];
if (!timestampString) {
timestampString = [[theProperties objectForKey:(id)kCGImagePropertyTIFFDictionary]
objectForKey:(id)kCGImagePropertyTIFFDateTime];
}
}
NSDate* itemDate = nil;
NSTimeZone* itemTimeZone = [[self source] timeZone];
if (timestampString) {
NSDateFormatter* timestampParser = [NSDateFormatter tl_tiffDateFormatter];
[timestampParser setTimeZone:itemTimeZone];
itemDate = [timestampParser dateFromString:timestampString];
}
if (itemDate) {
TLTimestamp* timestamp = [TLTimestamp timestampWithTime:itemDate
accuracy:TLTimestampAccuracyUnknown];
[theMetadata setObject:timestamp forKey:TLMetadataTimestampKey];
[theMetadata setObject:itemTimeZone forKey:TLMetadataTimezoneKey];
}
[self setMetadata:theMetadata];
}
+ (CFDictionaryRef)thumbnailOptionsForSize:(CGFloat)size {
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageAlways,
[NSNumber numberWithDouble:size], (id)kCGImageSourceThumbnailMaxPixelSize,
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, nil];
return (CFDictionaryRef)options;
}
- (CGImageRef)newThumbnailForSize:(CGFloat)approximateSize
options:(NSDictionary*)options
error:(NSError**)err
{
(void)approximateSize;
(void)options;
if (![self imageSource]) {
if (err) {
*err = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSFileReadCorruptFileError
userInfo:nil];
}
return NULL;
}
CFDictionaryRef thumbnailOptions = [[self class] thumbnailOptionsForSize:approximateSize];
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex([self imageSource], 0, thumbnailOptions);
if (!thumbnail && err) {
*err = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadCorruptFileError userInfo:nil];
}
return thumbnail;
}
@end