I have t开发者_如何学Gohis code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
...
}
Unfortunately, it runs the rest of the code (which can take a few seconds) before dismissing the modal view controller. How can i make it dismiss it first, and then run the rest of the code?
It won't dismiss the modal view controller or do any other UI updates until it gets back to the main run loop (i.e. the function returns).
If you have a task that is going to take a noticeable amount of time to complete, you should perform it on a background thread instead of on the main thread. The usual thing is to use performSelectorInBackground:withObject:
; but do remember that any UI updates (e.g. when the operation is complete) must be done on the main thread, so your background method may have to use performSelectorOnMainThread:withObject:waitUntilDone:
.
If you prefer blocks, you could use dispatch_async
instead:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
// background operation
});
If you have a number of background operations, you could also look into NSOperationQueue for the background operation.
If you dont need the animation, try:
[picker dismissModalViewControllerAnimated:NO];
精彩评论