Ray Wenderlich 提供了一个很棒的教程,介绍了如何使用大型中央调度和代码块:
http ://www.raywenderlich.com/1888/how-to-create-a-simple-iphone-app-tutorial-part-33
您也可以使用作业队列和 performSelector:onThread:withObject:waitUntilDone: if 块对您来说很可怕。
基本上,这个想法是在主线程上做最少的实际工作,因为这是绘制 UI 的地方。以下是 RW 的解决方案,状态部分已注释掉。那是您放置活动指示器的地方。
- (IBAction)addPictureTapped:(id)sender {
if (self.picker == nil) {
// 1) Show status
//[SVProgressHUD showWithStatus:@"Loading picker..."];
// 2) Get a concurrent queue form the system
dispatch_queue_t concurrentQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 3) Load picker in background
dispatch_async(concurrentQueue, ^{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker.allowsEditing = NO;
// 4) Present picker in main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController presentModalViewController:picker animated:YES];
[SVProgressHUD dismiss];
});
});
} else {
[self.navigationController presentModalViewController:picker animated:YES];
}
}