iOS终于知道为什么有时候打开相机是黑屏了
之前写一个相机,发现在有的手机上能够正常启动相机,而有的打开相机是黑屏。多处查询而未果,今天看到友盟微社区的代码,终于知道原因了。其实没有网上说的那么复杂,就是需要看一下在你手机的设置——隐私——相机中,本软件是不是允许访问相机,只要允许就可以正常打开。所以在写代码的时候要判断一下本程序是不是有访问相机的权限。代码如下:
注意:此方法只对ios7以上的系统有用,如果是在ios6的系统的话就直接崩溃了,况且ios6上也没有“设置–隐私–相机” 那一项
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted)
{
[[[UIAlertView alloc] initWithTitle:nil message:@”本应用无访问相机的权限,如需访问,可在设置中修改” delegate:nil cancelButtonTitle:@”好的” otherButtonTitles:nil, nil] show];
return;
}
}else{
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author == kCLAuthorizationStatusRestricted || author == kCLAuthorizationStatusDenied)
{
[[[UIAlertView alloc] initWithTitle:nil message:@”本应用无访问相机的权限,如需访问,可在设置中修改” delegate:nil cancelButtonTitle:@”好的” otherButtonTitles:nil, nil] show];
return;
}
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
— NORMAL —