(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 // create overlay
UIView *loadingView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIView *loadingView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
loadingView.clipsToBounds = YES; loadingView.clipsToBounds = YES;
// create loading spinner // create loading spinner
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.frame = CGRectMake(65, 40, activityView.bounds.size.width, activityView.bounds.size.height); activityView.frame = CGRectMake(65, 40, activityView.bounds.size.width, activityView.bounds.size.height);
activityView.center = loadingView.center; activityView.center = loadingView.center;
[loadingView addSubview:activityView]; [loadingView addSubview:activityView];
// create message // create message
UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)]; UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
loadingLabel.backgroundColor = [UIColor clearColor]; loadingLabel.backgroundColor = [UIColor clearColor];
loadingLabel.textColor = [UIColor whiteColor]; loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.adjustsFontSizeToFitWidth = YES; loadingLabel.adjustsFontSizeToFitWidth = YES;
CGPoint loadingLabelLocation = loadingView.center; CGPoint loadingLabelLocation = loadingView.center;
loadingLabelLocation.y += [activityView bounds].size.height; loadingLabelLocation.y += [activityView bounds].size.height;
loadingLabel.center = loadingLabelLocation; loadingLabel.center = loadingLabelLocation;
loadingLabel.textAlignment = UITextAlignmentCenter; loadingLabel.textAlignment = UITextAlignmentCenter;
loadingLabel.text = @"Processing assets..."; loadingLabel.text = @"Processing assets...";
[loadingLabel setFont:[UIFont boldSystemFontOfSize:18]]; [loadingLabel setFont:[UIFont boldSystemFontOfSize:18]];
[loadingView addSubview:loadingLabel]; [loadingView addSubview:loadingLabel];
// show all // show all
[mainView addSubview:loadingView]; [mainView addSubview:loadingView];
[activityView startAnimating]; [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) { if (video != nil) {
[selections addObject:video]; [selections addObject:video];
} }
processed++; processed++;
[lock unlock]; [lock unlock];
if (processed == [assets count]) { if (processed == [assets count]) {
self.resolve(selections); self.resolve(selections);
[indicatorView stopAnimating]; [indicatorView stopAnimating];
[overlayView removeFromSuperview]; [overlayView removeFromSuperview];
[imagePickerController dismissViewControllerAnimated:YES completion:nil]; [imagePickerController dismissViewControllerAnimated:YES completion:nil];
return; 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);
NSString *filePath = [self persistFile:data]; dispatch_async(dispatch_get_main_queue(), ^{
if (filePath == nil) { UIImage *image = [UIImage imageWithData:imageData];
self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil); NSData *data = UIImageJPEGRepresentation(image, 1);
[imagePickerController dismissViewControllerAnimated:YES completion:nil];
return;
}
[lock lock]; NSString *filePath = [self persistFile:data];
[selections addObject:[self createAttachmentResponse:filePath if (filePath == nil) {
withWidth:@(phAsset.pixelWidth) self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil);
withHeight:@(phAsset.pixelHeight) [imagePickerController dismissViewControllerAnimated:YES completion:nil];
withMime:@"image/jpeg" return;
withSize:[NSNumber numberWithUnsignedInteger:data.length] }
withData:[[self.options objectForKey:@"includeBase64"] boolValue] ? [data base64EncodedStringWithOptions:0] : [NSNull null]
]];
processed++;
[lock unlock];
if (processed == [assets count]) { [lock lock];
self.resolve(selections); [selections addObject:[self createAttachmentResponse:filePath
[indicatorView stopAnimating]; withWidth:@(phAsset.pixelWidth)
[overlayView removeFromSuperview]; withHeight:@(phAsset.pixelHeight)
[imagePickerController dismissViewControllerAnimated:YES completion:nil]; withMime:@"image/jpeg"
return; withSize:[NSNumber numberWithUnsignedInteger:data.length]
} withData:[[self.options objectForKey:@"includeBase64"] boolValue] ? [data base64EncodedStringWithOptions:0] : [NSNull null]
]];
processed++;
[lock unlock];
if (processed == [assets count]) {
self.resolve(selections);
[indicatorView stopAnimating];
[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]; [indicatorView stopAnimating];
[overlayView removeFromSuperview]; [overlayView removeFromSuperview];
[imagePickerController dismissViewControllerAnimated:YES completion:nil]; [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": {