最近在使用系统拍照,只要一点击拍照就崩溃,不稳定复现,最终抓到Log如下:[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.
最终定位到代码:
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
[self presentViewController:self.imagePickerController animated:YES completion:^{}];
}
}];
在回调里面进行present操作,Block块中present不是在主线程执行,会报错,需要在主线程进行present操作:
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:self.imagePickerController animated:YES completion:^{}]
});
}
}];