forked from meznaric/react-native-image-cropping
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReactNativeImageCropping.m
More file actions
146 lines (118 loc) · 5.71 KB
/
ReactNativeImageCropping.m
File metadata and controls
146 lines (118 loc) · 5.71 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
#import "ReactNativeImageCropping.h"
#import <UIKit/UIKit.h>
#import "TOCropViewController.h"
@interface ReactNativeImageCropping () <TOCropViewControllerDelegate>
@property (nonatomic, strong) RCTPromiseRejectBlock _reject;
@property (nonatomic, strong) RCTPromiseResolveBlock _resolve;
@property TOCropViewControllerAspectRatio aspectRatio;
@end
@implementation RCTConvert (AspectRatio)
RCT_ENUM_CONVERTER(TOCropViewControllerAspectRatio, (@{
@"AspectRatioOriginal" : @(TOCropViewControllerAspectRatioOriginal),
@"AspectRatioSquare" : @(TOCropViewControllerAspectRatioSquare),
@"AspectRatio3x2" : @(TOCropViewControllerAspectRatio3x2),
@"AspectRatio5x3" : @(TOCropViewControllerAspectRatio5x3),
@"AspectRatio4x3" : @(TOCropViewControllerAspectRatio4x3),
@"AspectRatio5x4" : @(TOCropViewControllerAspectRatio5x4),
@"AspectRatio7x5" : @(TOCropViewControllerAspectRatio7x5),
@"AspectRatio16x9" : @(TOCropViewControllerAspectRatio16x9)
}), UIStatusBarAnimationNone, integerValue)
@end
@implementation ReactNativeImageCropping
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
- (NSDictionary *)constantsToExport
{
return @{
@"AspectRatioOriginal" : @(TOCropViewControllerAspectRatioOriginal),
@"AspectRatioSquare" : @(TOCropViewControllerAspectRatioSquare),
@"AspectRatio3x2" : @(TOCropViewControllerAspectRatio3x2),
@"AspectRatio5x3" : @(TOCropViewControllerAspectRatio5x3),
@"AspectRatio4x3" : @(TOCropViewControllerAspectRatio4x3),
@"AspectRatio5x4" : @(TOCropViewControllerAspectRatio5x4),
@"AspectRatio7x5" : @(TOCropViewControllerAspectRatio7x5),
@"AspectRatio16x9" : @(TOCropViewControllerAspectRatio16x9),
};
}
RCT_EXPORT_METHOD( cropImageWithUrl:(NSString *)imageUrl
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject
)
{
self._reject = reject;
self._resolve = resolve;
self.aspectRatio = NULL;
NSString *fileURL = [[NSURL alloc] initFileURLWithPath:imageUrl].absoluteString;
NSURLRequest *imageUrlrequest = [NSURLRequest requestWithURL:[NSURL URLWithString:fileURL]];
[self.bridge.imageLoader loadImageWithURLRequest:imageUrlrequest callback:^(NSError *error, UIImage *image) {
if(error) reject(@"100", @"Failed to load image", error);
if(image) {
[self handleImageLoad:image];
}
}];
}
RCT_EXPORT_METHOD(cropImageWithUrlAndAspect:(NSString *)imageUrl
aspectRatio:(TOCropViewControllerAspectRatio)aspectRatio
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject
)
{
self._reject = reject;
self._resolve = resolve;
self.aspectRatio = aspectRatio;
NSString *fileURL = [[NSURL alloc] initFileURLWithPath:imageUrl].absoluteString;
NSURLRequest *imageUrlrequest = [NSURLRequest requestWithURL:[NSURL URLWithString:fileURL]];
[self.bridge.imageLoader loadImageWithURLRequest:imageUrlrequest callback:^(NSError *error, UIImage *image) {
if(error) reject(@"100", @"Failed to load image", error);
if(image) {
[self handleImageLoad:image];
}
}];
}
- (void)handleImageLoad:(UIImage *)image {
UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image];
cropViewController.delegate = self;
if(self.aspectRatio) {
cropViewController.lockedAspectRatio = YES;
cropViewController.defaultAspectRatio = self.aspectRatio;
}
dispatch_async(dispatch_get_main_queue(), ^{
[root presentViewController:cropViewController animated:YES completion:nil];
});
}
/**
Called when the user has committed the crop action, and provides both the original image with crop co-ordinates.
@param image The newly cropped image.
@param cropRect A rectangle indicating the crop region of the image the user chose (In the original image's local co-ordinate space)
*/
- (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle
{
dispatch_async(dispatch_get_main_queue(), ^{
[cropViewController dismissViewControllerAnimated:YES completion:nil];
});
NSData *pngData = UIImagePNGRepresentation(image);
NSString *fileName = [NSString stringWithFormat:@"memegenerator-crop-%lf.png", [NSDate timeIntervalSinceReferenceDate]];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName]; //Add the file name)
[pngData writeToFile:filePath atomically:YES];
NSNumber *width = [NSNumber numberWithFloat:image.size.width];
NSNumber *height = [NSNumber numberWithFloat:image.size.height];
NSDictionary * imageData = @{
@"uri":filePath,
@"width":width,
@"height":height
};
self._resolve(imageData);
}
/**
If implemented, when the user hits cancel, or completes a UIActivityViewController operation, this delegate will be called,
giving you a chance to manually dismiss the view controller
*/
- (void)cropViewController:(TOCropViewController *)cropViewController didFinishCancelled:(BOOL)cancelled
{
dispatch_async(dispatch_get_main_queue(), ^{
[cropViewController dismissViewControllerAnimated:YES completion:nil];
});
self._reject(@"400", @"Cancelled", [NSError errorWithDomain:@"Cancelled" code:400 userInfo:NULL]);
}
@end