Why will not modalviewcontrolleranimated be rejected to release the UIImagePickerController immediately on ios5?

I have a UITableViewController that issues a UIImagePickerController, the user takes a snapshot, clicks the Use button, Picker dismisses to show its own thumbnail counter when the image is being processed, then the counter is replaced with the actual thumbnail at the end of the processing.

At least how it works in iOS4. Now with iOS5, he just sits there until he is done, and then everything works correctly. But I want this counter to be there, so that the user knows something that is happening, otherwise it looks like it was just hanging.

So, I have this:

- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex (NSInteger)buttonIndex { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = NO; // yada, yada, yada [self presentModalViewController:picker animated:YES]; [picker release]; } 

And then this is called when the user selects "Use":

 - (void) imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [self dismissModalViewControllerAnimated:YES]; [self performSelectorOnMainThread:@selector(processImage:) withObject:info waitUntilDone:NO]; animate = true; } 

And then it is called to perform processing while the thumbnail rotates:

 - (void) processImage:(NSDictionary *)info { UIImage *image = nil; NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType]; // processing of image animate = false; [activityImageView stopAnimating]; [activityImageView release]; [self.tableView reloadData]; } 

As I said, it worked great with iOS4, but with iOS5, there is no such luck. So what is the deal? In the end, the image picker is fired, so why doesn't he immediately quit?

+4
source share
2 answers

I do not know why there is a difference between iOS4 and iOS5 at this point. But your description of the user interface widget is consistent with the code you provided. The execution selector in the main thread does just that, executing the selector in the main thread, which is the thread you are calling from. Because of this parameter, waitUntilDone: to NO pointless, since it is not sent to another thread, it just works in order. You would probably get the results you want by simply replacing the order, like So:

 [self dismissModalViewControllerAnimated:YES]; animate = true; [self performSelectorOnMainThread:@selector(processImage:) withObject:info waitUntilDone:NO]; 

But keep in mind that at best this would be risky since I assume that // processing of image does not contain concurrency. I prefer blocks for concurrency. And besides, I like nested blocks to make concurrency easy to follow, for example:

 -(void)doSomeStuffInBackground{ // Prepare for background stuff dispatch_async(dispatch_get_global_queue(0, 0), ^{ // Do background stuff dispatch_async(dispatch_get_main_queue(), ^{ // Update UI from results of background stuff }); }); } 

So, with that in mind, I would suggest something like this:

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ [self dismissModalViewControllerAnimated:YES]; [self processImage:info]; } -(void)processImage:(NSDictionary *)info{ animate = true; UIImage *image = nil; NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ // processing of image here on background thread dispatch_async(dispatch_get_main_queue(), ^{ // update UI here on main thread animate = false; [activityImageView stopAnimating]; [activityImageView release]; [self.tableView reloadData]; }); }); } 

This will unload the main work into the background thread so that the user interface remains responsive.

+4
source

Try using

 [[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil]; 

instead:

 [[picker parentViewController] dismissModalViewControllerAnimated: YES]; 
0
source

Source: https://habr.com/ru/post/1385033/


All Articles