(iOS) Run UI code on main thread

This commit is contained in:
Ivan Pusic 2016-11-14 20:35:09 +01:00
parent 8c770b9f14
commit 9db793daa3
2 changed files with 101 additions and 93 deletions

View File

@ -214,9 +214,7 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
imagePickerController.mediaType = QBImagePickerMediaTypeAny; imagePickerController.mediaType = QBImagePickerMediaTypeAny;
} }
dispatch_async(dispatch_get_main_queue(), ^{ [[self getRootVC] presentViewController:imagePickerController animated:YES completion:nil];
[[self getRootVC] presentViewController:imagePickerController animated:YES completion:nil];
});
}); });
}]; }];
} }
@ -236,37 +234,39 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
} }
- (void)showActivityIndicator:(void (^)(UIActivityIndicatorView*, UIView*))handler { - (void)showActivityIndicator:(void (^)(UIActivityIndicatorView*, UIView*))handler {
UIView *mainView = [[self getRootVC] view]; dispatch_async(dispatch_get_main_queue(), ^{
UIView *mainView = [[self getRootVC] view];
// create overlay
UIView *loadingView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; // create overlay
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; UIView *loadingView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
loadingView.clipsToBounds = YES; loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
loadingView.clipsToBounds = YES;
// create loading spinner
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; // create loading spinner
activityView.frame = CGRectMake(65, 40, activityView.bounds.size.width, activityView.bounds.size.height); UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = loadingView.center; activityView.frame = CGRectMake(65, 40, activityView.bounds.size.width, activityView.bounds.size.height);
[loadingView addSubview:activityView]; activityView.center = loadingView.center;
[loadingView addSubview:activityView];
// create message
UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)]; // create message
loadingLabel.backgroundColor = [UIColor clearColor]; UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
loadingLabel.textColor = [UIColor whiteColor]; loadingLabel.backgroundColor = [UIColor clearColor];
loadingLabel.adjustsFontSizeToFitWidth = YES; loadingLabel.textColor = [UIColor whiteColor];
CGPoint loadingLabelLocation = loadingView.center; loadingLabel.adjustsFontSizeToFitWidth = YES;
loadingLabelLocation.y += [activityView bounds].size.height; CGPoint loadingLabelLocation = loadingView.center;
loadingLabel.center = loadingLabelLocation; loadingLabelLocation.y += [activityView bounds].size.height;
loadingLabel.textAlignment = UITextAlignmentCenter; loadingLabel.center = loadingLabelLocation;
loadingLabel.text = @"Processing assets..."; loadingLabel.textAlignment = UITextAlignmentCenter;
[loadingLabel setFont:[UIFont boldSystemFontOfSize:18]]; loadingLabel.text = @"Processing assets...";
[loadingView addSubview:loadingLabel]; [loadingLabel setFont:[UIFont boldSystemFontOfSize:18]];
[loadingView addSubview:loadingLabel];
// show all
[mainView addSubview:loadingView]; // show all
[activityView startAnimating]; [mainView addSubview:loadingView];
[activityView startAnimating];
handler(activityView, loadingView);
handler(activityView, loadingView);
});
} }
@ -356,56 +356,61 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
if (phAsset.mediaType == PHAssetMediaTypeVideo) { if (phAsset.mediaType == PHAssetMediaTypeVideo) {
[self getVideoAsset:phAsset completion:^(NSDictionary* video) { [self getVideoAsset:phAsset completion:^(NSDictionary* video) {
[lock lock]; dispatch_async(dispatch_get_main_queue(), ^{
[lock lock];
if (video != nil) {
[selections addObject:video]; if (video != nil) {
} [selections addObject:video];
}
processed++;
[lock unlock]; processed++;
[lock unlock];
if (processed == [assets count]) {
self.resolve(selections); if (processed == [assets count]) {
[indicatorView stopAnimating]; self.resolve(selections);
[overlayView removeFromSuperview]; [indicatorView stopAnimating];
[imagePickerController dismissViewControllerAnimated:YES completion:nil]; [overlayView removeFromSuperview];
return; [imagePickerController dismissViewControllerAnimated:YES completion:nil];
} return;
}
});
}]; }];
} else { } else {
[manager [manager
requestImageDataForAsset:phAsset requestImageDataForAsset:phAsset
options:options options:options
resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
UIImage *image = [UIImage imageWithData:imageData];
NSData *data = UIImageJPEGRepresentation(image, 1); dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
NSString *filePath = [self persistFile:data]; NSData *data = UIImageJPEGRepresentation(image, 1);
if (filePath == nil) {
self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil); NSString *filePath = [self persistFile:data];
[imagePickerController dismissViewControllerAnimated:YES completion:nil]; if (filePath == nil) {
return; self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil);
} [imagePickerController dismissViewControllerAnimated:YES completion:nil];
return;
[lock lock]; }
[selections addObject:[self createAttachmentResponse:filePath
withWidth:@(phAsset.pixelWidth) [lock lock];
withHeight:@(phAsset.pixelHeight) [selections addObject:[self createAttachmentResponse:filePath
withMime:@"image/jpeg" withWidth:@(phAsset.pixelWidth)
withSize:[NSNumber numberWithUnsignedInteger:data.length] withHeight:@(phAsset.pixelHeight)
withData:[[self.options objectForKey:@"includeBase64"] boolValue] ? [data base64EncodedStringWithOptions:0] : [NSNull null] withMime:@"image/jpeg"
]]; withSize:[NSNumber numberWithUnsignedInteger:data.length]
processed++; withData:[[self.options objectForKey:@"includeBase64"] boolValue] ? [data base64EncodedStringWithOptions:0] : [NSNull null]
[lock unlock]; ]];
processed++;
if (processed == [assets count]) { [lock unlock];
self.resolve(selections);
[indicatorView stopAnimating]; if (processed == [assets count]) {
[overlayView removeFromSuperview]; self.resolve(selections);
[imagePickerController dismissViewControllerAnimated:YES completion:nil]; [indicatorView stopAnimating];
return; [overlayView removeFromSuperview];
} [imagePickerController dismissViewControllerAnimated:YES completion:nil];
return;
}
});
}]; }];
} }
} }
@ -416,16 +421,18 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
[self showActivityIndicator:^(UIActivityIndicatorView *indicatorView, UIView *overlayView) { [self showActivityIndicator:^(UIActivityIndicatorView *indicatorView, UIView *overlayView) {
if (phAsset.mediaType == PHAssetMediaTypeVideo) { if (phAsset.mediaType == PHAssetMediaTypeVideo) {
[self getVideoAsset:phAsset completion:^(NSDictionary* video) { [self getVideoAsset:phAsset completion:^(NSDictionary* video) {
if (video != nil) { dispatch_async(dispatch_get_main_queue(), ^{
self.resolve(video); if (video != nil) {
} else { self.resolve(video);
self.reject(ERROR_CANNOT_PROCESS_VIDEO_KEY, ERROR_CANNOT_PROCESS_VIDEO_MSG, nil); } else {
} self.reject(ERROR_CANNOT_PROCESS_VIDEO_KEY, ERROR_CANNOT_PROCESS_VIDEO_MSG, nil);
}
[indicatorView stopAnimating];
[overlayView removeFromSuperview]; [indicatorView stopAnimating];
[imagePickerController dismissViewControllerAnimated:YES completion:nil]; [overlayView removeFromSuperview];
[imagePickerController dismissViewControllerAnimated:YES completion:nil];
});
}]; }];
} else { } else {
[manager [manager
@ -434,10 +441,11 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
resultHandler:^(NSData *imageData, NSString *dataUTI, resultHandler:^(NSData *imageData, NSString *dataUTI,
UIImageOrientation orientation, UIImageOrientation orientation,
NSDictionary *info) { NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
[indicatorView stopAnimating]; [indicatorView stopAnimating];
[overlayView removeFromSuperview]; [overlayView removeFromSuperview];
[self processSingleImagePick:[UIImage imageWithData:imageData] withViewController:imagePickerController]; [self processSingleImagePick:[UIImage imageWithData:imageData] withViewController:imagePickerController];
});
}]; }];
} }
}]; }];

View File

@ -1,6 +1,6 @@
{ {
"name": "react-native-image-crop-picker", "name": "react-native-image-crop-picker",
"version": "0.10.2", "version": "0.10.3",
"description": "Select single or multiple images, with croping option", "description": "Select single or multiple images, with croping option",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {