This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
CDVImageEditor.m
273 lines (236 loc) · 9.83 KB
/
CDVImageEditor.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
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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import <AdobeCreativeSDKImage/AdobeCreativeSDKImage.h>
#import "CDVImageEditor.h"
#define ADB_PHOTO_PREFIX @"adb_photo_"
#define isEqualIgnoreCaseToString(string1, string2) ([string1 caseInsensitiveCompare:string2] == NSOrderedSame)
@implementation CDVImageEditor
@synthesize callbackId;
@synthesize imageUri;
@synthesize encodingType;
@synthesize quality;
- (void)edit:(CDVInvokedUrlCommand*)command
{
self.callbackId = command.callbackId;
self.imageUri = [command.arguments objectAtIndex:0];
self.encodingType = [command.arguments objectAtIndex:1];
NSArray *tools = [self createToolArray:[command.arguments objectAtIndex:2]];
self.quality = [[command.arguments objectAtIndex:3] integerValue] != 100 ? [command.arguments objectAtIndex:3] : [NSNumber numberWithInt:100];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageUri]];
UIImage *image = [UIImage imageWithData:imageData];
AdobeUXImageEditorViewController *editorController =
[[AdobeUXImageEditorViewController alloc] initWithImage:image];
[editorController setDelegate:self];
if ([tools count] > 0) {
[AdobeImageEditorCustomization setToolOrder:tools];
}
BOOL custom = [[command.arguments objectAtIndex:11] boolValue];
[AdobeImageEditorCustomization setCropToolCustomEnabled: custom];
if (custom) {
NSArray *crops = [self createCustomCropArray: [command.arguments objectAtIndex:14]];
[AdobeImageEditorCustomization setCropToolPresets: crops];
}
BOOL invert = [[command.arguments objectAtIndex:12] boolValue];
[AdobeImageEditorCustomization setCropToolInvertEnabled: invert];
BOOL orig = [[command.arguments objectAtIndex:13] boolValue];
[AdobeImageEditorCustomization setCropToolOriginalEnabled: orig];
NSArray *orientations = [command.arguments objectAtIndex:15];
[AdobeImageEditorCustomization setSupportedIpadOrientations:orientations];
// setup buttons
[AdobeImageEditorCustomization setLeftNavigationBarButtonTitle:[self getLeftButtonLabel:[command.arguments objectAtIndex:16]]];
[AdobeImageEditorCustomization setRightNavigationBarButtonTitle:[self getRightButtonLabel:[command.arguments objectAtIndex:17]]];
[self.viewController presentViewController:editorController animated:YES completion:nil];
}
- (void)photoEditor:(AdobeUXImageEditorViewController *)editor finishedWithImage:(UIImage *)image
{
CDVPluginResult *pluginResult = nil;
NSString *extension = [self.encodingType integerValue] == EncodingTypePNG ? @"png" : @"jpg";
NSData* data = [self processImage:image];
if (data) {
NSString* filePath = [self tempFilePath:extension];
NSError* err = nil;
// save file
if (![data writeToFile:filePath options:NSAtomicWrite error:&err]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[NSURL fileURLWithPath:filePath] absoluteString]];
}
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)photoEditorCanceled:(AdobeUXImageEditorViewController *)editor
{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
}
- (NSString*)tempFilePath:(NSString*)extension
{
NSString* docsPath = [NSTemporaryDirectory()stringByStandardizingPath];
NSFileManager* fileMgr = [[NSFileManager alloc] init]; // recommended by Apple (vs [NSFileManager defaultManager]) to be threadsafe
NSString* filePath;
// generate unique file name
int i = 1;
do {
filePath = [NSString stringWithFormat:@"%@/%@%03d.%@", docsPath, ADB_PHOTO_PREFIX, i++, extension];
} while ([fileMgr fileExistsAtPath:filePath]);
return filePath;
}
- (NSData*)processImage:(UIImage*)image
{
NSData* data = nil;
if ([self.encodingType integerValue] == EncodingTypePNG) {
data = UIImagePNGRepresentation(image);
} else if ([self.encodingType integerValue] == EncodingTypeJPEG) {
data = UIImageJPEGRepresentation(image, [self.quality floatValue] / 100.0f);
}
return data;
}
- (NSString *) getLeftButtonLabel:(NSNumber*)buttonIndex
{
NSString *label = kAFLeftNavigationTitlePresetCancel;
int buttonId = [buttonIndex integerValue];
switch(buttonId) {
case LeftButtonTypeBack:
label = kAFLeftNavigationTitlePresetBack;
break;
case LeftButtonTypeExit:
label = kAFLeftNavigationTitlePresetExit;
break;
case LeftButtonTypeCancel:
default:
label = kAFLeftNavigationTitlePresetCancel;
break;
}
return label;
}
- (NSString *) getRightButtonLabel:(NSNumber*)buttonIndex
{
NSString *label = kAFRightNavigationTitlePresetDone;
int buttonId = [buttonIndex integerValue];
switch(buttonId) {
case RightButtonTypeSave:
label = kAFRightNavigationTitlePresetSave;
break;
case RightButtonTypeNext:
label = kAFRightNavigationTitlePresetNext;
break;
case RightButtonTypeSend:
label = kAFRightNavigationTitlePresetSend;
break;
case RightButtonTypeDone:
default:
label = kAFRightNavigationTitlePresetDone;
break;
}
return label;
}
- (NSArray*) createToolArray:(NSArray*)toolOptions
{
NSMutableArray *tools = [NSMutableArray array];
for (NSNumber *tempNumber in toolOptions) {
int toolId = [tempNumber integerValue];
NSLog(@"Single element: %d", toolId);
switch(toolId){
case ToolTypeSharpness:
[tools addObject: kAdobeImageEditorSharpness];
break;
case ToolTypeEffects:
[tools addObject: kAdobeImageEditorEffects];
break;
case ToolTypeRedeye:
[tools addObject: kAdobeImageEditorRedeye];
break;
case ToolTypeCrop:
[tools addObject: kAdobeImageEditorCrop];
break;
case ToolTypeWhiten:
[tools addObject: kAdobeImageEditorWhiten];
break;
case ToolTypeDraw:
[tools addObject: kAdobeImageEditorDraw];
break;
case ToolTypeStickers:
[tools addObject: kAdobeImageEditorStickers];
break;
case ToolTypeText:
[tools addObject: kAdobeImageEditorText];
break;
case ToolTypeBlemish:
[tools addObject: kAdobeImageEditorBlemish];
break;
case ToolTypeMeme:
[tools addObject: kAdobeImageEditorMeme];
break;
case ToolTypeOrientation:
[tools addObject: kAdobeImageEditorOrientation];
break;
case ToolTypeEnhance:
[tools addObject: kAdobeImageEditorEnhance];
break;
case ToolTypeFrames:
[tools addObject: kAdobeImageEditorFrames];
break;
case ToolTypeSplash:
[tools addObject: kAdobeImageEditorSplash];
break;
case ToolTypeFocus:
[tools addObject: kAdobeImageEditorFocus];
break;
case ToolTypeBlur:
[tools addObject: kAdobeImageEditorBlur];
break;
case ToolTypeVignette:
[tools addObject: kAdobeImageEditorVignette];
break;
case ToolTypeLighting:
[tools addObject: kAdobeImageEditorLightingAdjust];
break;
case ToolTypeColor:
[tools addObject: kAdobeImageEditorColorAdjust];
break;
case ToolTypeOverlays:
[tools addObject: kAdobeImageEditorOverlay];
break;
/*
* apparently the adjust tool is only on Android
*
case ToolTypeAdjust:
[tools addObject: xxxxx];
break;
*/
default:
// Ignore any tool not from the above
break;
}
}
return [tools copy];
}
- (NSArray*) createCustomCropArray:(NSArray*)cropOptions
{
NSMutableArray *crops = [NSMutableArray array];
for (NSDictionary *tempCrop in cropOptions) {
NSMutableDictionary *crop = [NSMutableDictionary dictionaryWithCapacity: 3];
[crop setObject:[tempCrop objectForKey:@"label"] forKey:kAFCropPresetName];
[crop setObject:[tempCrop objectForKey:@"width"] forKey:kAFCropPresetWidth];
[crop setObject:[tempCrop objectForKey:@"height"] forKey:kAFCropPresetHeight];
[crops addObject: crop];
}
return [crops copy];
}
@end