Cordova Camera Plugin Takes Forever to Fire
Cordova Camera Picker Takes a Long Time to Load. Fast and Easy Fix.
While trying to fire the camera plugin on cordova i realized the action was taking forever to fire about 30-40 seconds. I saw a bug on console and immediately started doing research. I found a useful stackoverflow, but it was not as clear so i decided to leave a clear written example of how it should be fixed.
1. Locate Plugins/CDVCamera.m
2. Locate showCameraPicker
3. You need to include the top code into the dispatch_async
4. Here is how the function should look.
- (void)showCameraPicker:(NSString*)callbackId withOptions:(CDVPictureOptions *) pictureOptions { // CDVCameraPicker* cameraPicker = [CDVCameraPicker createFromPictureOptions:pictureOptions]; // self.pickerController = cameraPicker; // cameraPicker.delegate = self; // cameraPicker.callbackId = callbackId; // // we need to capture this state for memory warnings that dealloc this object // cameraPicker.webView = self.webView; // Perform UI operations on the main thread dispatch_async(dispatch_get_main_queue(), ^{ CDVCameraPicker* cameraPicker = [CDVCameraPicker createFromPictureOptions:pictureOptions]; self.pickerController = cameraPicker; cameraPicker.delegate = self; cameraPicker.callbackId = callbackId; // we need to capture this state for memory warnings that dealloc this object cameraPicker.webView = self.webView; // If a popover is already open, close it; we only want one at a time. if (([[self pickerController] pickerPopoverController] != nil) && [[[self pickerController] pickerPopoverController] isPopoverVisible]) { [[[self pickerController] pickerPopoverController] dismissPopoverAnimated:YES]; [[[self pickerController] pickerPopoverController] setDelegate:nil]; [[self pickerController] setPickerPopoverController:nil]; } if ([self popoverSupported] && (pictureOptions.sourceType != UIImagePickerControllerSourceTypeCamera)) { if (cameraPicker.pickerPopoverController == nil) { cameraPicker.pickerPopoverController = [[NSClassFromString(@"UIPopoverController") alloc] initWithContentViewController:cameraPicker]; } [self displayPopover:pictureOptions.popoverOptions]; self.hasPendingOperation = NO; } else { cameraPicker.modalPresentationStyle = UIModalPresentationCurrentContext; [self.viewController presentViewController:cameraPicker animated:YES completion:^{ self.hasPendingOperation = NO; }]; } }); }
Source: https://stackoverflow.com/questions/58038810/xcode-10-main-thread-checker-cordova-camera-plugin