問題:
在調(diào)用相機時髓堪,進行了權(quán)限請求送朱,發(fā)生了Crash。
原因:
在相機權(quán)限請求回調(diào)中干旁,不在主線程驶沼,故喚起UIImagePickerController時發(fā)生崩潰。
分析:
1争群、相機的權(quán)限請求
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
//The completion handler is called on an arbitrary dispatch queue.
//It is the client's responsibility to ensure that any UIKit-related updates are called on the main queue or main thread as a result.
//意思是:completionHandler會在任意隊列上執(zhí)行回怜,我們要確保在主線程中執(zhí)行。
}
But换薄,為什么測試沒有測出來玉雾?因為在已經(jīng)授權(quán)時,這個handler會在主線程中執(zhí)行轻要;
那相冊權(quán)限呢复旬?
2、相冊權(quán)限請求
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
//Photos may call your handler block on an arbitrary serial queue.
//If your handler needs to interact with UI elements, dispatch such work to the main queue.
//在官方api中的note敘述如上冲泥,可知驹碍,與相機類似。
}];
解決:
很簡單了凡恍,在回掉中,切到主線程中嚼酝。
PS:附帶三種回主線程的方法浮还。
參考:https://blog.csdn.net/cordova/article/details/54933729
// 1.NSThread
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
- (void)updateUI {
}
// 2.NSOperationQueue
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
// 3.GCD
dispatch_async(dispatch_get_main_queue(), ^{
});