出于某种原因,我第一次在我的应用程序中以相机模式打开UIImagePickerController
时,它显示为空白。我必须关闭并重新打开该视图才能让相机进纸开始工作。我正在使用适用于iOS 6的标准代码来完美捕捉相机。从下面的示例中,我正在触发capturePhoto:
方法。还有其他人在使用iOS 7相机时遇到这种尴尬吗?我检查了Apple dev论坛,但几乎不可能在那里找到答案。
- (IBAction)capturePhoto:(id)sender {
[self doImagePickerForType:UIImagePickerControllerSourceTypeCamera];
}
- (void)doImagePickerForType:(UIImagePickerControllerSourceType)type {
if (!_imagePicker) {
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.mediaTypes = @[(NSString*)kUTTypeImage];
_imagePicker.delegate = self;
}
_imagePicker.sourceType = type;
[self presentViewController:_imagePicker animated:YES completion:nil];
}
答案 0 :(得分:16)
我也在使用UIImagePickerController,并在空白屏幕上遇到了同样的问题。我想稍微介绍一下klaudz提到的关于相机的iOS 7授权的内容。
“录制音频始终需要用户明确许可;录制视频还需要用户在某些地区销售的设备的许可。”
以下是一些代码片段,您可以查看是否拥有相机的权限,并在您的应用先前未请求时请求它。如果您因先前的请求而被拒绝,您的应用可能需要向用户发出通知,进入设置以手动启用访问权限,正如klaudz所指出的那样。
iOS 7示例
NSString *mediaType = AVMediaTypeVideo; // Or AVMediaTypeAudio
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
// This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.
if(authStatus == AVAuthorizationStatusRestricted){
NSLog(@"Restricted");
}
// The user has explicitly denied permission for media capture.
else if(authStatus == AVAuthorizationStatusDenied){
NSLog(@"Denied");
}
// The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question.
else if(authStatus == AVAuthorizationStatusAuthorized){
NSLog(@"Authorized");
}
// Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.
else if(authStatus == AVAuthorizationStatusNotDetermined){
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
// Make sure we execute our code on the main thread so we can update the UI immediately.
//
// See documentation for ABAddressBookRequestAccessWithCompletion where it says
// "The completion handler is called on an arbitrary queue."
//
// Though there is no similar mention for requestAccessForMediaType, it appears it does
// the same thing.
//
dispatch_async(dispatch_get_main_queue(), ^{
if(granted){
// UI updates as needed
NSLog(@"Granted access to %@", mediaType);
}
else {
// UI updates as needed
NSLog(@"Not granted access to %@", mediaType);
}
});
}];
}
else {
NSLog(@"Unknown authorization status");
}
答案 1 :(得分:7)
在iOS 7中,应用可以在获得用户授权之前访问相机。
当应用程序第一次访问摄像头时,iOS会显示警告视图以询问用户。
用户还可以在Settings--Privacy--Camera--[Your app's name]
中设置授权。
如果开关关闭,相机将保持黑色空白视图。
如果使用AVCaptureDeviceInput
来调用相机,可以查看:
NSError *inputError = nil;
AVCaptureDeviceInput *captureInput =
[AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&inputError];
if (inputError &&
inputError.code == AVErrorApplicationIsNotAuthorizedToUseDevice)
{
// not authorized
}
如果您使用UIImagePickerController
致电,我仍在寻找检查是否获得授权的方法。
我试过这两种方法:
[UIImagePickerController isSourceTypeAvailable:]
[UIImagePickerController isCameraDeviceAvailable:]
但他们没有工作,他们都返回YES。
<强>更新强>
感谢Scott的扩张。 [AVCaptureDevice authorizationStatusForMediaType:]
是一种更好的检查方法。
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusAuthorized) {
// successful
} else {
// failed, such as
// AVAuthorizationStatusNotDetermined
// AVAuthorizationStatusRestricted
// AVAuthorizationStatusNotDetermined
}
但请记得查看iOS版本,因为iOS 7上方可以使用[AVCaptureDevice authorizationStatusForMediaType:]
和AVAuthorizationStatus
。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
// code for AVCaptureDevice auth checking
}
答案 2 :(得分:4)
我遇到了完全相同的问题,并尝试了互联网上的每一个解决方案,没有运气。但最后我发现背景线程阻止了相机预览显示。如果您在尝试打开相机时碰巧有后台线程运行,请尝试阻止后台线程,看看会发生什么。希望你能解决它。
答案 3 :(得分:0)
我遇到了这个控件AQPhotoPicker。它很容易使用,希望它能帮到你