-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTLImageCaptureItem.m
More file actions
312 lines (275 loc) · 10.2 KB
/
TLImageCaptureItem.m
File metadata and controls
312 lines (275 loc) · 10.2 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// TLImageCaptureItem.m
// Geotagalog
//
// Created by Nathan Vander Wilt on 6/2/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLImageCaptureItem.h"
#include "TLImageCapture.h"
#import "TLImageCapturePhotoSource.h"
#import "TLTimestamp.h"
#import "NSURL+TLExtensions.h"
#import "NSFileManager+TLExtensions.h"
#import "NSDateFormatter+TLAdditions.h"
extern NSString* TLMakeUUID(void);
// denotes location of 0-th row, 0-th column
typedef enum {
TLImageOrientationTopLeft = 1, // default orientation
UIImageOrientationTopRight, // flip horizontally (across |)
TLImageOrientationBottomRight, // rotate 180 deg
TLImageOrientationBottomLeft, // flip vertically (across -)
TLImageOrientationLeftTop, // rotate right, then flip vertically
TLImageOrientationRightTop, // rotate right (90 deg CW)
TLImageOrientationRightBottom, // rotate left, then flip vertically
TLImageOrientationLeftBottom // rotate left (90 deg CCW)
} TLImageOrientation;
static CGImageRef TLCGImageCreateOriented(CGImageRef srcImg, TLImageOrientation orientation);
@interface TLImageCaptureItem ()
@property (nonatomic, assign) TLICAObject icao;
@property (nonatomic, assign) UInt32 fileType;
@property (nonatomic, copy) NSString* originalFilename;
@property (nonatomic, copy) NSDictionary* metadata;
- (void)updateWithInfo:(NSDictionary*)theInfo;
- (NSURL*)exportToFolder:(NSURL*)targetDirectory
error:(NSError**)err
shouldRename:(BOOL)shouldRename;
@end
@implementation TLImageCaptureItem
@synthesize icao;
@synthesize fileType;
@synthesize metadata;
@synthesize originalFilename;
-(id)initWithSource:(TLImageCapturePhotoSource*)theSource
info:(NSDictionary*)theInfo
{
self = [super initWithSource:theSource];
if (self) {
//NSLog(@"%@", theInfo);
TLICAObject theIcao = [[theInfo objectForKey:(id)kTLICAObjectKey] intValue];
[self setIcao:theIcao];
[self updateWithInfo:theInfo];
}
return self;
}
- (void)dealloc {
[self setOriginalFilename:nil];
[self setMetadata:nil];
[super dealloc];
}
- (void)updateWithInfo:(NSDictionary*)theInfo {
NSString* theFilename = [theInfo objectForKey:(id)kTLICAObjectNameKey];
if (theFilename) {
[self setOriginalFilename:theFilename];
}
NSNumber* itemType = [theInfo objectForKey:(id)kTLICAFileTypeKey];
if (itemType) {
[self setFileType:[itemType intValue]];
}
NSMutableDictionary* theMetadata = [[[self metadata] mutableCopy] autorelease];
if (!theMetadata) {
theMetadata = [NSMutableDictionary dictionary];
}
NSString* timestampString = [theInfo objectForKey:(id)kTLICAImageDateOriginalKey];
if (!timestampString) {
timestampString = [theInfo objectForKey:(id)kTLICAImageDateDigitizedKey];
}
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];
}
- (NSString*)originalUTI {
NSString* uti = nil;
switch ([self fileType]) {
case kICAFileImage:
uti = (id)kUTTypeImage;
break;
case kICAFileMovie:
uti = (id)kUTTypeMovie;
break;
case kICAFileAudio:
uti = (id)kUTTypeAudio;
break;
default:
uti = (id)kUTTypeData;
break;
}
return uti;
}
- (CGImageRef)newThumbnailForSize:(CGFloat)approximateSize
options:(NSDictionary*)options
error:(NSError**)err
{
(void)approximateSize;
(void)options;
(void)err;
CFDataRef thumbnailData = NULL;
ICACopyObjectThumbnailPB copyThumbnailPB = {};
copyThumbnailPB.object = [self icao];
copyThumbnailPB.thumbnailFormat = kICAThumbnailFormatJPEG;
copyThumbnailPB.thumbnailData = &thumbnailData;
ICAError internalErrCode = TLICACopyObjectThumbnail(©ThumbnailPB, NULL);
if (internalErrCode) {
if (err) {
*err = [NSError errorWithDomain:NSOSStatusErrorDomain code:internalErrCode userInfo:nil];
}
return NULL;
}
// fetch the object's properties so we can display thumbnails properly (see note below)
CFDictionaryRef objectProperties = NULL;
ICACopyObjectPropertyDictionaryPB copyPropertiesPB = {};
copyPropertiesPB.object = [self icao];
copyPropertiesPB.theDict = &objectProperties;
internalErrCode = TLICACopyObjectPropertyDictionary(©PropertiesPB, NULL);
if (internalErrCode) {
if (err) {
*err = [NSError errorWithDomain:NSOSStatusErrorDomain code:internalErrCode userInfo:nil];
}
return NULL;
}
NSNumber* orientationValue = (id)CFDictionaryGetValue(objectProperties, kTLICAImageOrientationKey);
CFRelease(objectProperties);
CGImageRef thumbnail = NULL;
CGImageSourceRef thumbnailSource = CGImageSourceCreateWithData(thumbnailData, NULL);
CFRelease(thumbnailData);
if (thumbnailSource) {
if (CGImageSourceGetCount(thumbnailSource)) {
thumbnail = CGImageSourceCreateImageAtIndex(thumbnailSource, 0, NULL);
/* NOTE: ICA does not autorotate thumbnail rdar://problem/6959922
So we must rotate it ourselves. */
if (orientationValue) {
TLImageOrientation orientation = [orientationValue unsignedIntValue];
CGImageRef orientedThumbnail = TLCGImageCreateOriented(thumbnail, orientation);
CGImageRelease(thumbnail);
thumbnail = orientedThumbnail;
}
}
CFRelease(thumbnailSource);
}
if (!thumbnail && err) {
*err = [NSError errorWithDomain:NSOSStatusErrorDomain code:readErr userInfo:nil];
}
return thumbnail;
}
- (NSURL*)exportUniquedIntoFolder:(NSURL*)targetDirectory
error:(NSError**)err
{
NSFileManager* fileManager = [NSFileManager tl_threadManager];
NSString* uniqueFolder = [NSString stringWithFormat:@".com.calftrail-%@", TLMakeUUID()];
NSURL* subfolder = [targetDirectory tl_URLByAppendingPathComponent:uniqueFolder];
BOOL createdTempDir = [fileManager createDirectoryAtPath:[subfolder path]
withIntermediateDirectories:NO
attributes:nil
error:err];
if (!createdTempDir) return nil;
NSURL* uniquedExport = nil;
NSURL* temporaryDownloadedURL = [self exportToFolder:subfolder
error:err
shouldRename:NO];
if (temporaryDownloadedURL) {
NSURL* desiredTarget = [targetDirectory tl_URLByAppendingPathComponent:
[temporaryDownloadedURL tl_lastPathComponent]];
NSString* finalPath = [fileManager tl_moveItemAtPath:[temporaryDownloadedURL path]
toUniquePath:[desiredTarget path]
error:err];
if (finalPath) {
uniquedExport = [NSURL fileURLWithPath:finalPath isDirectory:NO];
}
}
(void)[fileManager removeItemAtPath:[subfolder path]
error:NULL];
return uniquedExport;
}
- (NSURL*)exportToFolder:(NSURL*)targetDirectory
error:(NSError**)err
shouldRename:(BOOL)shouldRename
{
FSRef downloadHostFolder = {};
Boolean success = CFURLGetFSRef((CFURLRef)targetDirectory, &downloadHostFolder);
if (!success) {
if (err) {
NSDictionary* errInfo = [NSDictionary dictionaryWithObjectsAndKeys:
targetDirectory, NSURLErrorKey, nil];
*err = [NSError errorWithDomain:NSPOSIXErrorDomain code:ENOENT userInfo:errInfo];
}
return nil;
}
FSRef downloadedFile = {};
ICADownloadFilePB downloadFilePB = {};
downloadFilePB.object = [self icao];
downloadFilePB.flags = kAdjustCreationDate;
downloadFilePB.dirFSRef = &downloadHostFolder;
downloadFilePB.fileFSRef = &downloadedFile;
/* NOTE: if large file cancellation is desired in the future,
or finer control of output location, check out ICACopyObjectData() */
ICAError internalErrCode = TLICADownloadFile(&downloadFilePB, NULL);
if (internalErrCode == dupFNErr && shouldRename) {
/* NOTE: Image Capture will fail if a file with the same name already exists.
So we have to do our own conflict avoidance. */
return [self exportUniquedIntoFolder:targetDirectory error:err];
}
else if (internalErrCode) {
if (err) {
*err = [NSError errorWithDomain:NSOSStatusErrorDomain code:internalErrCode userInfo:nil];
}
return NO;
}
NSURL* downloadedURL = (id)CFURLCreateFromFSRef(kCFAllocatorDefault, &downloadedFile);
return [downloadedURL autorelease];
}
- (NSURL*)exportToFolder:(NSURL*)targetDirectory
error:(NSError**)err
{
return [self exportToFolder:targetDirectory error:err shouldRename:YES];
}
@end
CGImageRef TLCGImageCreateOriented(CGImageRef srcImg, TLImageOrientation orientation) {
// Only handle common rotations (for now). See http://www.gotow.net/creative/wordpress/?p=64
CGSize srcSize = CGSizeMake(CGImageGetWidth(srcImg), CGImageGetHeight(srcImg));
CGAffineTransform drawTransform = CGAffineTransformIdentity;
BOOL flipAspect = NO;
switch (orientation) {
default:
case TLImageOrientationTopLeft:
return CGImageRetain(srcImg);
case TLImageOrientationBottomRight:
drawTransform = CGAffineTransformMakeTranslation(srcSize.width, srcSize.height);
drawTransform = CGAffineTransformRotate(drawTransform, (CGFloat)M_PI);
break;
case TLImageOrientationRightTop:
drawTransform = CGAffineTransformMakeTranslation(0.0f, srcSize.width);
drawTransform = CGAffineTransformRotate(drawTransform, (CGFloat)-M_PI_2);
flipAspect = YES;
break;
case TLImageOrientationLeftBottom:
drawTransform = CGAffineTransformMakeTranslation(srcSize.height, 0.0f);
drawTransform = CGAffineTransformRotate(drawTransform, (CGFloat)M_PI_2);
flipAspect = YES;
break;
}
CGSize targetSize = (flipAspect ?
CGSizeMake(CGImageGetHeight(srcImg), CGImageGetWidth(srcImg)) :
CGSizeMake(CGImageGetWidth(srcImg), CGImageGetHeight(srcImg)));
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(NULL, targetSize.width, targetSize.height,
8, (size_t)targetSize.width * 4,
space, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(space);
NSCAssert(ctx, @"Couldn't create image flipping context");
CGContextConcatCTM(ctx, drawTransform);
CGContextDrawImage(ctx, (CGRect){.origin = CGPointZero, .size = srcSize}, srcImg);
CGImageRef img = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
return img;
}