@@ -29,21 +29,21 @@ - (instancetype)init
2929 @" height" : @200
3030 };
3131 }
32-
32+
3333 return self;
3434}
3535
3636RCT_EXPORT_METHOD (openPicker:(NSDictionary *)options
3737 resolver:(RCTPromiseResolveBlock)resolve
3838 rejecter:(RCTPromiseRejectBlock)reject) {
39-
39+
4040 self.resolve = resolve;
4141 self.reject = reject;
4242 self.options = [NSMutableDictionary dictionaryWithDictionary: self .defaultOptions];
4343 for (NSString *key in options.keyEnumerator ) {
4444 [self .options setValue: options[key] forKey: key];
4545 }
46-
46+
4747 [PHPhotoLibrary requestAuthorization: ^(PHAuthorizationStatus status) {
4848 dispatch_async (dispatch_get_main_queue (), ^{
4949 // init picker
@@ -54,7 +54,7 @@ - (instancetype)init
5454 imagePickerController.maximumNumberOfSelection = [[self .options objectForKey: @" maxFiles" ] intValue ];
5555 imagePickerController.showsNumberOfSelectedAssets = YES ;
5656 imagePickerController.mediaType = QBImagePickerMediaTypeImage;
57-
57+
5858 UIViewController *root = [[[[UIApplication sharedApplication ] delegate ]
5959 window ] rootViewController ];
6060 [root presentViewController: imagePickerController
@@ -67,67 +67,76 @@ - (instancetype)init
6767- (void )qb_imagePickerController :
6868(QBImagePickerController *)imagePickerController
6969 didFinishPickingAssets : (NSArray *)assets {
70-
70+
7171 PHImageManager *manager = [PHImageManager defaultManager ];
72-
72+
7373 if ([[[self options ] objectForKey: @" multiple" ] boolValue ]) {
7474 NSMutableArray *images = [[NSMutableArray alloc ] init ];
7575 PHImageRequestOptions* options = [[PHImageRequestOptions alloc ] init ];
7676 options.synchronous = YES ;
77-
77+
7878 for (PHAsset *asset in assets) {
7979 [manager
8080 requestImageDataForAsset: asset
8181 options: options
8282 resultHandler: ^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
83- NSString *filePath = [self persistFile: imageData];
83+ UIImage *image = [UIImage imageWithData: imageData];
84+ NSData *data = UIImageJPEGRepresentation (image, 1 );
85+
86+ NSString *filePath = [self persistFile: data];
8487 if (filePath == nil ) {
8588 self.reject (ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil );
8689 return ;
8790 }
88-
91+
8992 [images addObject: @{
9093 @" path" : filePath,
9194 @" width" : @(asset.pixelWidth ),
92- @" height" : @(asset.pixelHeight )
95+ @" height" : @(asset.pixelHeight ),
96+ @" mime" : @" image/jpeg" ,
97+ @" size" : [NSNumber numberWithUnsignedInteger: data.length]
9398 }];
9499 }];
95100 }
96-
101+
97102 self.resolve (images);
98103 [imagePickerController dismissViewControllerAnimated: YES completion: nil ];
99104 } else {
100105 PHAsset *asset = [assets objectAtIndex: 0 ];
101-
106+
102107 [manager
103108 requestImageDataForAsset: asset
104109 options: nil
105110 resultHandler: ^(NSData *imageData, NSString *dataUTI,
106111 UIImageOrientation orientation,
107112 NSDictionary *info) {
108-
113+
109114 if ([[[self options ] objectForKey: @" cropping" ] boolValue ]) {
110115 UIImage *image = [UIImage imageWithData: imageData];
111116 RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc ] initWithImage: image cropMode: RSKImageCropModeCustom];
112-
117+
113118 imageCropVC.avoidEmptySpaceAroundImage = YES ;
114119 imageCropVC.dataSource = self;
115120 imageCropVC.delegate = self;
116-
121+
117122 UIViewController *root = [[[[UIApplication sharedApplication ] delegate ] window ] rootViewController ];
118123 [imagePickerController dismissViewControllerAnimated: YES completion: nil ];
119124 [root presentViewController: imageCropVC animated: YES completion: nil ];
120125 } else {
121- NSString *filePath = [self persistFile: imageData];
126+ UIImage *image = [UIImage imageWithData: imageData];
127+ NSData *data = UIImageJPEGRepresentation (image, 1 );
128+ NSString *filePath = [self persistFile: data];
122129 if (filePath == nil ) {
123130 self.reject (ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil );
124131 return ;
125132 }
126-
133+
127134 self.resolve (@{
128135 @" path" : filePath,
129136 @" width" : @(asset.pixelWidth ),
130- @" height" : @(asset.pixelHeight )
137+ @" height" : @(asset.pixelHeight ),
138+ @" mime" : @" image/jpeg" ,
139+ @" size" : [NSNumber numberWithUnsignedInteger: data.length]
131140 });
132141 [imagePickerController dismissViewControllerAnimated: YES completion: nil ];
133142 }
@@ -148,14 +157,14 @@ - (CGRect)imageCropViewControllerCustomMaskRect:
148157 CGSize maskSize = CGSizeMake (
149158 [[self .options objectForKey: @" width" ] intValue ],
150159 [[self .options objectForKey: @" height" ] intValue ]);
151-
160+
152161 CGFloat viewWidth = CGRectGetWidth (controller.view .frame );
153162 CGFloat viewHeight = CGRectGetHeight (controller.view .frame );
154-
163+
155164 CGRect maskRect = CGRectMake ((viewWidth - maskSize.width ) * 0 .5f ,
156165 (viewHeight - maskSize.height ) * 0 .5f ,
157166 maskSize.width , maskSize.height );
158-
167+
159168 return maskRect;
160169}
161170
@@ -165,7 +174,7 @@ - (CGRect) scaleRect:(RSKImageCropViewController *)controller {
165174 CGRect rect = controller.maskRect ;
166175 CGFloat viewWidth = CGRectGetWidth (controller.view .frame );
167176 CGFloat viewHeight = CGRectGetHeight (controller.view .frame );
168-
177+
169178 if (rect.size .width > viewWidth) {
170179 float scaleFactor = viewWidth / rect.size .width ;
171180 rect.size .width *= scaleFactor;
@@ -179,7 +188,7 @@ - (CGRect) scaleRect:(RSKImageCropViewController *)controller {
179188 rect.origin .x = viewWidth / 2 * 0 .5f ;
180189 rect.origin .y = 0 ;
181190 }
182-
191+
183192 return rect;
184193}
185194
@@ -212,25 +221,27 @@ - (void)imageCropViewControllerDidCancelCrop:
212221- (void )imageCropViewController : (RSKImageCropViewController *)controller
213222 didCropImage : (UIImage *)croppedImage
214223 usingCropRect : (CGRect)cropRect {
215-
224+
216225 // we have correct rect, but not correct dimensions
217226 // so resize image
218227 CGSize resizedImageSize = CGSizeMake ([[[self options ] objectForKey: @" width" ] intValue ], [[[self options ] objectForKey: @" height" ] intValue ]);
219228 UIImage *resizedImage = [croppedImage resizedImageToFitInSize: resizedImageSize scaleIfSmaller: YES ];
220229 NSData *data = UIImageJPEGRepresentation (resizedImage, 1 );
221-
230+
222231 NSString *filePath = [self persistFile: data];
223232 if (filePath == nil ) {
224233 self.reject (ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil );
225234 return ;
226235 }
227-
236+
228237 NSDictionary *image = @{
229238 @" path" : filePath,
230239 @" width" : @(resizedImage.size .width ),
231- @" height" : @(resizedImage.size .height )
240+ @" height" : @(resizedImage.size .height ),
241+ @" mime" : @" image/jpeg" ,
242+ @" size" : [NSNumber numberWithUnsignedInteger: data.length]
232243 };
233-
244+
234245 self.resolve (image);
235246 [controller dismissViewControllerAnimated: YES completion: nil ];
236247}
@@ -241,13 +252,13 @@ - (NSString*) persistFile:(NSData*)data {
241252 // create temp file
242253 NSString *filePath = [NSTemporaryDirectory () stringByAppendingString: [[NSUUID UUID ] UUIDString ]];
243254 filePath = [filePath stringByAppendingString: @" .jpg" ];
244-
255+
245256 // save cropped file
246257 BOOL status = [data writeToFile: filePath atomically: YES ];
247258 if (!status) {
248259 return nil ;
249260 }
250-
261+
251262 return filePath;
252263}
253264
0 commit comments