-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathTiImagefactoryModule.m
More file actions
259 lines (215 loc) · 7.69 KB
/
TiImagefactoryModule.m
File metadata and controls
259 lines (215 loc) · 7.69 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
/**
* Ti.Imagefactory Module
* Copyright (c) 2011-2013 by Appcelerator, Inc. All Rights Reserved.
* Please see the LICENSE included with this distribution for details.
*/
#import "TiImagefactoryModule.h"
#import "TiBase.h"
#import "TiBlob.h"
#import "TiHost.h"
#import "TiImageFactory.h"
#import "TiUtils.h"
@implementation TiImagefactoryModule
#pragma mark Internal
- (id)moduleGUID
{
return @"0aab25d7-0486-40ab-94a3-ed4f9a293414";
}
- (NSString *)moduleId
{
return @"ti.imagefactory";
}
#pragma System Properties
typedef enum {
kTransformNone = 0,
kTransformCrop,
kTransformResize,
kTransformThumbnail,
kTransformRoundedCorner,
kTransformTransparentBorder,
kTransformAlpha,
kTransformRotate
} TransformType;
MAKE_SYSTEM_PROP(TRANSFORM_CROP, kTransformCrop);
MAKE_SYSTEM_PROP(TRANSFORM_RESIZE, kTransformResize);
MAKE_SYSTEM_PROP(TRANSFORM_THUMBNAIL, kTransformThumbnail);
MAKE_SYSTEM_PROP(TRANSFORM_ROUNDEDCORNER, kTransformRoundedCorner);
MAKE_SYSTEM_PROP(TRANSFORM_TRANSPARENTBORDER, kTransformTransparentBorder);
MAKE_SYSTEM_PROP(TRANSFORM_ALPHA, kTransformAlpha);
MAKE_SYSTEM_PROP(TRANSFORM_ROTATE, kTransformRotate);
MAKE_SYSTEM_PROP(QUALITY_DEFAULT, kCGInterpolationDefault);
MAKE_SYSTEM_PROP(QUALITY_NONE, kCGInterpolationNone);
MAKE_SYSTEM_PROP(QUALITY_LOW, kCGInterpolationLow);
MAKE_SYSTEM_PROP(QUALITY_MEDIUM, kCGInterpolationMedium);
MAKE_SYSTEM_PROP(QUALITY_HIGH, kCGInterpolationHigh);
#pragma mark Image Transform Helpers
+ (UIImage *)imageTransform:(TransformType)type image:(UIImage *)image withArgs:(id)args
{
switch (type) {
case kTransformCrop:
return [TiImageFactory imageCrop:image withArgs:args];
case kTransformResize:
return [TiImageFactory imageResize:image withArgs:args];
case kTransformThumbnail:
return [TiImageFactory imageThumbnail:image withArgs:args];
case kTransformRoundedCorner:
return [TiImageFactory imageRoundedCorner:image withArgs:args];
case kTransformTransparentBorder:
return [TiImageFactory imageTransparentBorder:image withArgs:args];
case kTransformAlpha:
return [TiImageFactory imageAlpha:image withArgs:args];
case kTransformRotate:
return [TiImageFactory imageRotate:image withArgs:args];
case kTransformNone:
default:
return image;
}
}
+ (id)imageTransform:(TransformType)type withArgs:(id)args
{
enum Args {
kArgBlob = 0,
kArgOptions,
kArgCount
};
// Validate correct number of arguments
ENSURE_ARG_COUNT(args, kArgCount);
id blob = [args objectAtIndex:kArgBlob];
ENSURE_TYPE(blob, TiBlob);
UIImage *image = [TiImagefactoryModule imageTransform:type image:[(TiBlob *)blob image] withArgs:[args objectAtIndex:kArgOptions]];
return image ? [[[TiBlob alloc] initWithImage:image] autorelease] : nil;
}
#pragma mark Public Image Methods
- (id)imageWithRotation:(id)args
{
return [TiImagefactoryModule imageTransform:kTransformRotate withArgs:args];
}
- (id)imageWithAlpha:(id)args
{
return [TiImagefactoryModule imageTransform:kTransformAlpha withArgs:args];
}
- (id)imageWithTransparentBorder:(id)args
{
return [TiImagefactoryModule imageTransform:kTransformTransparentBorder withArgs:args];
}
- (id)imageWithRoundedCorner:(id)args
{
return [TiImagefactoryModule imageTransform:kTransformRoundedCorner withArgs:args];
}
- (id)imageAsThumbnail:(id)args
{
return [TiImagefactoryModule imageTransform:kTransformThumbnail withArgs:args];
}
- (id)imageAsResized:(id)args
{
return [TiImagefactoryModule imageTransform:kTransformResize withArgs:args];
}
- (id)imageAsCropped:(id)args
{
return [TiImagefactoryModule imageTransform:kTransformCrop withArgs:args];
}
- (id)imageAsUpright:(id)args
{
ENSURE_SINGLE_ARG(args, TiBlob);
TiBlob *blob = (TiBlob *)args;
UIImage *sourceImage = [blob image];
UIImage *newImage = [TiImageFactory imageUpright:sourceImage];
if (newImage && (newImage != sourceImage)) {
blob = [[[TiBlob alloc] initWithImage:newImage] autorelease];
}
return blob;
}
- (id)imageTransform:(id)args
{
enum Args {
kArgBlob = 0,
kArgTransforms,
kArgCount
};
// Validate correct number of arguments
ENSURE_ARG_COUNT(args, kArgCount);
id blob = [args objectAtIndex:kArgBlob];
ENSURE_TYPE(blob, TiBlob);
UIImage *image = [(TiBlob *)blob image];
for (id transform in args) {
if ([transform isKindOfClass:[NSDictionary class]]) {
TransformType type = (TransformType)[TiUtils intValue:@"type" properties:transform def:kTransformNone];
image = [TiImagefactoryModule imageTransform:type image:image withArgs:transform];
}
}
return image ? [[[TiBlob alloc] initWithImage:image] autorelease] : nil;
}
- (id)compress:(id)args
{
if (![args[0] isKindOfClass:[NSDictionary class]]) {
DEPRECATED_REPLACED(@"ImageFactory.compress(file, quality)", @"3.0.0", @"ImageFactory.compress({ file, quality, callback })");
TiBlob *blob = nil;
NSNumber *qualityObject = @-1;
ENSURE_ARG_AT_INDEX(blob, args, 0, TiBlob);
ENSURE_ARG_AT_INDEX(qualityObject, args, 1, NSNumber);
UIImage *image = [blob image];
image = [TiImageFactory imageUpright:image];
float qualityValue = [TiUtils floatValue:qualityObject def:1.0];
return [[[TiBlob alloc] initWithData:UIImageJPEGRepresentation(image, qualityValue) mimetype:@"image/jpeg"] autorelease];
}
ENSURE_SINGLE_ARG(args, NSDictionary);
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
TiBlob *blob = args[@"file"];
float quality = [TiUtils floatValue:@"quality" properties:args def:1.0];
KrollCallback *callback = args[@"callback"];
UIImage *image = [TiImageFactory imageUpright:[blob image]];
TiBlob *result = [[[TiBlob alloc] initWithData:UIImageJPEGRepresentation(image, quality) mimetype:@"image/jpeg"] autorelease];
TiThreadPerformOnMainThread(
^{
[callback call:@[ @{ @"file" : result } ] thisObject:self];
},
NO);
});
}
- (id)compressToFile:(id)args
{
// Fetch arguments.
TiBlob *blob = nil;
NSNumber *qualityObject = @-1;
NSString *filePath = nil;
ENSURE_ARG_AT_INDEX(blob, args, 0, TiBlob);
ENSURE_ARG_AT_INDEX(qualityObject, args, 1, NSNumber);
ENSURE_ARG_AT_INDEX(filePath, args, 2, NSString);
// Fetch blob's image in upright form.
UIImage *image = [blob image];
image = [TiImageFactory imageUpright:image];
// Compress the image to the format specified by the given path's file extension.
NSData *compressedData = nil;
NSString *fileExtension = [filePath pathExtension];
if ([fileExtension caseInsensitiveCompare:@"png"] == 0) {
compressedData = UIImagePNGRepresentation(image);
} else {
float qualityValue = [TiUtils floatValue:qualityObject def:1.0];
compressedData = UIImageJPEGRepresentation(image, qualityValue);
}
if (compressedData == nil) {
return NUMBOOL(NO);
}
// Create the directory tree if it doesn't already exist.
NSURL *fileUrl = [TiUtils toURL:filePath proxy:self];
NSURL *parentDirUrl = [fileUrl URLByDeletingLastPathComponent];
[NSFileManager.defaultManager createDirectoryAtURL:parentDirUrl withIntermediateDirectories:YES attributes:nil error:nil];
// Write to compressed image data to file.
NSError *err = nil;
[compressedData writeToURL:fileUrl options:NSDataWritingAtomic error:&err];
if (err != nil) {
NSLog(@"[ERROR] ImageFactory.compressToFile() failed for path \"%@\" - reason: %@", fileUrl, err);
}
return NUMBOOL(err != nil);
}
- (id)metadataFrom:(id)args
{
ENSURE_SINGLE_ARG(args, TiBlob);
TiBlob *blob = (TiBlob *)args;
NSData *imageData = [blob data];
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
NSDictionary *exifData = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(sourceRef, 0, NULL);
CFRelease(sourceRef);
return [exifData autorelease];
}
@end