今天项目遇到一个问题,就是我的项目中用到了
AVCaptureDevice
因为要做一个人脸识别的界面,不能用到系统带的摄像头,只能自定义界面,所以遇到一个问题,就是我进入这个界面要询问摄像头权限,然后呢,点击允许以后呢就出问题了,界面不能刷新到获取摄像头的界面,也就是停留在了白屏界面,等待一段时间可能会刷新过来,但是不能等啊
所以就百度了一下,还真有这种情况,套用别人的话“原因是操作UI没有在主线程。”
解决方法也很明显了,把UI加载放到主线程就可以了,上代码,粉色代码为主要代码(看备注)
csdn不知道为什么我发源码会出用白条隔开的,只能不要格式了,请大家见谅
//获取授权
- (void)getAuthorization {
/*
AVAuthorizationStatusNotDetermined = 0,// 未进行授权选择
AVAuthorizationStatusRestricted,// 未授权,且用户无法更新,如家长控制情况下
AVAuthorizationStatusDenied,// 用户拒绝App使用
AVAuthorizationStatusAuthorized,// 已授权,可使用
*/
//此处获取摄像头授权
switch([AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeVideo])
{
caseAVAuthorizationStatusAuthorized: //已授权,可使用 The client is authorized to access the hardware supporting a media type.
{
NSLog(@"授权摄像头使用成功");
[selfsetupAVCaptureInfo];
break;
}
caseAVAuthorizationStatusNotDetermined: //未进行授权选择 Indicates that the user has not yet made a choice regarding whether the client can access the hardware.
{
//则再次请求授权
[AVCaptureDevicerequestAccessForMediaType:AVMediaTypeVideocompletionHandler:^(BOOLgranted) {
if(granted){ //用户授权成功
dispatch_async(dispatch_get_main_queue(), ^{
//获取摄像头权限成功后,加载AVCaptureDevice控件,放到主线程就ok了
[selfsetupAVCaptureInfo]; //不放到主线程界面第一时间刷新不了
});
// [self setupAVCaptureInfo];
return;
} else{ //用户拒绝授权
[selfpop];
// [self showMsgWithTitle:@"出错了" andContent:@"用户拒绝授权摄像头的使用权,返回上一页.请打开\n设置-->隐私/通用等权限设置"];
return;
}
}];
break;
}
default: //用户拒绝授权/未授权
{
// [self pop];
[selfshowMsgWithTitle:@"出错了"andContent:@"拒绝授权,返回上一页.请检查下\n设置-->隐私/通用等权限设置"];
break;
}
}
}
作者原文链接:https://segmentfault.com/q/1010000006677073