/
EXImageManipulatorModule.m
363 lines (310 loc) · 13.8 KB
/
EXImageManipulatorModule.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright 2018-present 650 Industries. All rights reserved.
#import <EXImageManipulator/EXImageManipulatorModule.h>
#import <UMFileSystemInterface/UMFileSystemInterface.h>
#import <UMImageLoaderInterface/UMImageLoaderInterface.h>
#import <Photos/Photos.h>
@interface EXImageManipulatorModule ()
@property (nonatomic, weak) UMModuleRegistry *moduleRegistry;
@property (nonatomic, weak) id<UMFileSystemInterface> fileSystem;
@property (nonatomic, weak) id<UMImageLoaderInterface> imageLoader;
@end
static NSString* const ACTION_KEY_RESIZE = @"resize";
static NSString* const ACTION_KEY_ROTATE = @"rotate";
static NSString* const ACTION_KEY_FLIP = @"flip";
static NSString* const ACTION_KEY_CROP = @"crop";
static NSString* const SAVE_OPTIONS_KEY_FORMAT = @"format";
static NSString* const SAVE_OPTIONS_KEY_COMPRESS = @"compress";
static NSString* const SAVE_OPTIONS_KEY_BASE64 = @"base64";
@implementation EXImageManipulatorModule
UM_EXPORT_MODULE(ExpoImageManipulator);
- (void)setModuleRegistry:(UMModuleRegistry *)moduleRegistry
{
_moduleRegistry = moduleRegistry;
_fileSystem = [moduleRegistry getModuleImplementingProtocol:@protocol(UMFileSystemInterface)];
_imageLoader = [moduleRegistry getModuleImplementingProtocol:@protocol(UMImageLoaderInterface)];
}
UM_EXPORT_METHOD_AS(manipulateAsync,
uri:(NSString *)uri
actions:(NSArray *)actions
saveOptions:(NSDictionary *)saveOptions
resolve:(UMPromiseResolveBlock)resolve
reject:(UMPromiseRejectBlock)reject)
{
NSURL *url = [NSURL URLWithString:uri];
// no scheme provided in uri, handle as a local path and add 'file://' scheme
if (!url.scheme) {
url = [NSURL fileURLWithPath:uri isDirectory:false];
}
NSString *path = [url.path stringByStandardizingPath];
if (!_fileSystem) {
return reject(@"E_MISSING_MODULE", @"No FileSystem module.", nil);
}
if (!([_fileSystem permissionsForURI:url] & UMFileSystemPermissionRead)) {
return reject(@"E_FILESYSTEM_PERMISSIONS", [NSString stringWithFormat:@"File '%@' isn't readable.", uri], nil);
}
NSString *errorMessage;
if (![self areActionsValid:actions errorMessage:&errorMessage] || ![self areSaveOptionsValid:saveOptions errorMessage:&errorMessage]) {
return reject(@"E_IMAGE_MANIPULATOR_INVALID_ARG", errorMessage, nil);
}
if ([[url scheme] isEqualToString:@"assets-library"]) {
PHFetchResult<PHAsset *> *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
if (fetchResult.count > 0) {
PHAsset *asset = fetchResult[0];
CGSize size = CGSizeMake([asset pixelWidth], [asset pixelHeight]);
PHImageRequestOptions *options = [PHImageRequestOptions new];
[options setResizeMode:PHImageRequestOptionsResizeModeExact];
[options setNetworkAccessAllowed:YES];
[options setSynchronous:NO];
[options setDeliveryMode:PHImageRequestOptionsDeliveryModeHighQualityFormat];
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
if (!image) {
reject(@"E_IMAGE_MANIPULATION_FAILED", [NSString stringWithFormat:@"The file isn't convertable to image. Given path: `%@`.", path], nil);
return;
}
image = [self fixOrientation:image];
[self manipulateImage:image actions:actions saveOptions:saveOptions resolver:resolve rejecter:reject];
}];
return;
} else {
return reject(@"E_IMAGE_MANIPULATION_FAILED", [NSString stringWithFormat:@"The file does not exist. Given path: `%@`.", path], nil);
}
} else {
[_imageLoader loadImageForURL:url completionHandler:^(NSError *error, UIImage *loadedImage) {
if (error != nil) {
return reject(@"E_IMAGE_MANIPULATION_FAILED", @"Could not get the image", error);
}
UIImage *image = [self fixOrientation:loadedImage];
[self manipulateImage:image actions:actions saveOptions:saveOptions resolver:resolve rejecter:reject];
}];
}
}
-(BOOL)areActionsValid:(NSArray *)actions errorMessage:(NSString **)errorMessage
{
for (NSDictionary *action in actions) {
int actionsCounter = 0;
if (action[ACTION_KEY_RESIZE]) {
actionsCounter += 1;
}
if (action[ACTION_KEY_ROTATE]) {
actionsCounter += 1;
}
if (action[ACTION_KEY_FLIP]) {
actionsCounter += 1;
}
if (action[ACTION_KEY_CROP]) {
actionsCounter += 1;
if (action[ACTION_KEY_CROP][@"originX"] == nil
|| action[ACTION_KEY_CROP][@"originY"] == nil
|| action[ACTION_KEY_CROP][@"width"] == nil
|| action[ACTION_KEY_CROP][@"height"] == nil
) {
*errorMessage = @"Invalid crop options has been passed. Please make sure the object contains originX, originY, width and height.";
return NO;
}
}
if (actionsCounter != 1) {
*errorMessage = [NSString stringWithFormat:@"Single action must contain exactly one transformation from list: ['%@', '%@', '%@', '%@']",
ACTION_KEY_RESIZE,
ACTION_KEY_ROTATE,
ACTION_KEY_FLIP,
ACTION_KEY_CROP];
return NO;
}
}
return YES;
}
-(BOOL)areSaveOptionsValid:(NSDictionary *)saveOptions errorMessage:(NSString **)errorMessage
{
NSString* format = saveOptions[SAVE_OPTIONS_KEY_FORMAT];
if (![format isEqualToString:@"jpeg"] && ![format isEqualToString:@"png"]) {
*errorMessage = [NSString stringWithFormat:@"SaveOption 'format' must be one of ['png', 'jpeg']. Obtained '%@'.", format];
return NO;
}
return YES;
}
-(UIImage *)fixOrientation:(UIImage *)image
{
if (image.imageOrientation == UIImageOrientationUp) {
return image;
}
CGAffineTransform transform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, image.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, image.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height, CGImageGetBitsPerComponent(image.CGImage), 0, CGImageGetColorSpace(image.CGImage), CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.height, image.size.width), image.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
break;
}
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
-(void)manipulateImage:(UIImage *)image
actions:(NSArray *)actions
saveOptions:(NSDictionary *)saveOptions
resolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject
{
for (NSDictionary *action in actions) {
if (action[ACTION_KEY_RESIZE]) {
image = [self resizeImage:image options:action[ACTION_KEY_RESIZE]];
} else if (action[ACTION_KEY_ROTATE]) {
image = [self rotateImage:image rotation:(NSNumber *)action[ACTION_KEY_ROTATE]];
} else if (action[ACTION_KEY_FLIP]) {
image = [self flipImage:image flipType:action[ACTION_KEY_FLIP]];
} else if (action[ACTION_KEY_CROP]) {
NSString *errorMessage;
image = [self cropImage:image options:action[ACTION_KEY_CROP] didFailWithErrorMessage:&errorMessage];
if (errorMessage != nil) {
return reject(@"E_INVALID_CROP_DATA", errorMessage, nil);
}
}
}
float compressionValue = saveOptions[SAVE_OPTIONS_KEY_COMPRESS] != nil ? [(NSNumber *)saveOptions[SAVE_OPTIONS_KEY_COMPRESS] floatValue] : 1.0;
NSString *format = saveOptions[SAVE_OPTIONS_KEY_FORMAT];
NSData *imageData = nil;
NSString *extension;
if ([format isEqualToString:@"jpeg"]) {
imageData = UIImageJPEGRepresentation(image, compressionValue);
extension = @".jpg";
} else if ([format isEqualToString:@"png"]) {
imageData = UIImagePNGRepresentation(image);
extension = @".png";
}
NSString *directory = [_fileSystem.cachesDirectory stringByAppendingPathComponent:@"ImageManipulator"];
[_fileSystem ensureDirExistsWithPath:directory];
NSString *fileName = [[[NSUUID UUID] UUIDString] stringByAppendingString:extension];
NSString *newPath = [directory stringByAppendingPathComponent:fileName];
[imageData writeToFile:newPath atomically:YES];
NSURL *fileURL = [NSURL fileURLWithPath:newPath];
NSString *filePath = [fileURL absoluteString];
NSMutableDictionary *response = [NSMutableDictionary new];
response[@"uri"] = filePath;
response[@"width"] = @(CGImageGetWidth(image.CGImage));
response[@"height"] = @(CGImageGetHeight(image.CGImage));
if (saveOptions[SAVE_OPTIONS_KEY_BASE64] && [saveOptions[SAVE_OPTIONS_KEY_BASE64] boolValue]) {
response[@"base64"] = [imageData base64EncodedStringWithOptions:0];
}
resolve(response);
}
- (UIImage *)resizeImage:(UIImage *)image options:(NSDictionary *)resize
{
float imageWidth = image.size.width;
float imageHeight = image.size.height;
float imageRatio = imageWidth / imageHeight;
NSInteger requestedWidth = 0;
NSInteger requestedHeight = 0;
if (resize[@"width"]) {
requestedWidth = [(NSNumber *)resize[@"width"] integerValue];
requestedHeight = requestedWidth / imageRatio;
}
if (resize[@"height"]) {
requestedHeight = [(NSNumber *)resize[@"height"] integerValue];
requestedWidth = requestedWidth == 0 ? imageRatio * requestedHeight : requestedWidth;
}
CGSize requestedSize = CGSizeMake(requestedWidth, requestedHeight);
UIGraphicsBeginImageContextWithOptions(requestedSize, NO, 1.0);
[image drawInRect:CGRectMake(0, 0, requestedWidth, requestedHeight)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)rotateImage:(UIImage *)image rotation:(NSNumber *)rotation
{
float rads = [rotation integerValue] * M_PI / 180;
CGSize size = image.size;
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,size.width, size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(rads);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM(bitmap, rads);
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), image.CGImage);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)flipImage:(UIImage *)image flipType:(NSString *)flip
{
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:image];
UIGraphicsBeginImageContext(tempImageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform;
if ([flip isEqualToString:@"vertical"]) {
transform = CGAffineTransformMake(1, 0, 0, -1, 0, tempImageView.frame.size.height);
CGContextConcatCTM(context, transform);
} else if ([flip isEqualToString:@"horizontal"]) {
transform = CGAffineTransformMake(-1, 0, 0, 1, tempImageView.frame.size.width, 0);
CGContextConcatCTM(context, transform);
}
[tempImageView.layer renderInContext:context];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)cropImage:(UIImage *)image options:(NSDictionary *)cropData didFailWithErrorMessage:(NSString **)errorMessage
{
float originX = [(NSNumber *)cropData[@"originX"] floatValue];
float originY = [(NSNumber *)cropData[@"originY"] floatValue];
float requestedWidth = [(NSNumber *)cropData[@"width"] floatValue];
float requestedHeight = [(NSNumber *)cropData[@"height"] floatValue];
if (originX > image.size.width
|| originY > image.size.height
|| requestedWidth > image.size.width
|| requestedHeight > image.size.height
) {
*errorMessage = @"Invalid crop options has been passed. Please make sure the requested crop rectangle is inside source image.";
return nil;
}
CGRect cropDimensions = CGRectMake(originX, originY, requestedWidth, requestedHeight);
CGImageRef takenCGImage = image.CGImage;
CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropDimensions);
image = [UIImage imageWithCGImage:cropCGImage scale:image.scale orientation:image.imageOrientation];
CGImageRelease(cropCGImage);
return image;
}
@end