在我的应用程序中,我需要在点击按钮时显示 UIImagePickerController。这是我在控制器上的按钮被按下时调用的方法中使用的代码:
- (IBAction)choosePressed:(id)sender {
if (!self.pickerController) self.pickerController = [[UIImagePickerController alloc]init];
self.pickerController.delegate = self;
self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.navigationController presentViewController:self.pickerController animated:YES completion:nil];
}
问题是 UIImagePickerControllers 加载速度很慢,所以我认为在viewDidAppear:animated
方法中移动选取器的初始化,可能在另一个线程中,将是加快选取器创建/显示过程的好方法,所以我这样做了:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
dispatch_queue_t myQueue = dispatch_queue_create("Picker Queue", NULL);
dispatch_async(myQueue, ^{
self.pickerController = [[UIImagePickerController alloc]init];
self.pickerController.delegate = self;
});
}
- (IBAction)choosePressed:(id)sender {
self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.navigationController presentViewController:self.pickerController animated:YES completion:nil];
}
这样,当按下按钮时,pickerController 会立即显示,但是当加载主控制器时,UI 会冻结一点(可能是因为 pickerController 的初始化),但是由于我使用了 dispatch_async,所以应该在另一个线程中完成初始化机制不应该吗?我的代码有什么错误吗?
我对 GCD 很陌生,所以我一定遗漏了一些东西!