-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathCustomCropManager.mm
47 lines (33 loc) · 2.39 KB
/
CustomCropManager.mm
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
#import "CustomCropManager.h"
#import <React/RCTLog.h>
@implementation CustomCropManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(crop:(NSDictionary *)points imageUri:(NSString *)imageUri callback:(RCTResponseSenderBlock)callback)
{
NSString *parsedImageUri = [imageUri stringByReplacingOccurrencesOfString:@"file://" withString:@""];
NSURL *fileURL = [NSURL fileURLWithPath:parsedImageUri];
CIImage *ciImage = [CIImage imageWithContentsOfURL:fileURL];
CGPoint newLeft = CGPointMake([points[@"topLeft"][@"x"] floatValue], [points[@"topLeft"][@"y"] floatValue]);
CGPoint newRight = CGPointMake([points[@"topRight"][@"x"] floatValue], [points[@"topRight"][@"y"] floatValue]);
CGPoint newBottomLeft = CGPointMake([points[@"bottomLeft"][@"x"] floatValue], [points[@"bottomLeft"][@"y"] floatValue]);
CGPoint newBottomRight = CGPointMake([points[@"bottomRight"][@"x"] floatValue], [points[@"bottomRight"][@"y"] floatValue]);
newLeft = [self cartesianForPoint:newLeft height:[points[@"height"] floatValue] ];
newRight = [self cartesianForPoint:newRight height:[points[@"height"] floatValue] ];
newBottomLeft = [self cartesianForPoint:newBottomLeft height:[points[@"height"] floatValue] ];
newBottomRight = [self cartesianForPoint:newBottomRight height:[points[@"height"] floatValue] ];
NSMutableDictionary *rectangleCoordinates = [[NSMutableDictionary alloc] init];
rectangleCoordinates[@"inputTopLeft"] = [CIVector vectorWithCGPoint:newLeft];
rectangleCoordinates[@"inputTopRight"] = [CIVector vectorWithCGPoint:newRight];
rectangleCoordinates[@"inputBottomLeft"] = [CIVector vectorWithCGPoint:newBottomLeft];
rectangleCoordinates[@"inputBottomRight"] = [CIVector vectorWithCGPoint:newBottomRight];
ciImage = [ciImage imageByApplyingFilter:@"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimage = [context createCGImage:ciImage fromRect:[ciImage extent]];
UIImage *image = [UIImage imageWithCGImage:cgimage];
NSData *imageToEncode = UIImageJPEGRepresentation(image, 0.8);
callback(@[[NSNull null], @{@"image": [imageToEncode base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]}]);
}
- (CGPoint)cartesianForPoint:(CGPoint)point height:(float)height {
return CGPointMake(point.x, height - point.y);
}
@end