在我的应用中,我使用以下代码访问相机以更改用户的个人资料照片。但我无法访问相机。
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
if(isIOS8SystemVersion)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:picker animated:YES completion:NULL];
}];
}
else
{
[self presentViewController:picker animated:YES completion:NULL];
}
此代码适用于iOS 7.x的iPhone 4(16gb)。 但在iOS 9中,呈现的viewController显示为黑屏,我无法拍摄。与photoLibrary的情况相同 。相同的代码适用于同一设备中的其他应用。
我在Stack Overflow中经历了很多问题并尝试了很多解决方案。但是它们并不适用于我。就像为UIImagePIcker控制器等制作一个单例。等等。
在设置 - >隐私 - >相机 - > 中,我无法看到本节中列出的应用。
为了让iOS应用可以访问相机和照片,要做些什么呢?我错过了什么吗?
答案 0 :(得分:0)
只是延迟你的功能打开相机,它会正常工作:D 使用以下代码:
[self performSelector:@selector(showCamera) withObject:nil afterDelay:1.0];
-(void)showCamera
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
if(isIOS8SystemVersion)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:picker animated:YES completion:NULL];
}];
}
else
{
[self presentViewController:picker animated:YES completion:NULL];
}
}
答案 1 :(得分:0)
首先调用此方法并确定摄像机的权限
-(void)PermissionForCamera
{
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusAuthorized)
{
NSLog(@"AVAuthorizationStatusAuthorized");
// run your code for camera
ImagePicker = [[UIImagePickerController alloc] init];
[ImagePicker setDelegate:self];
ImagePicker = UIImagePickerControllerSourceTypeCamera;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self ImagePicker animated:YES completion:nil];
}];
}
else if(authStatus == AVAuthorizationStatusNotDetermined)
{
NSLog(@"AVAuthorizationStatusNotDetermined");
// run your code for camera
ImagePicker = [[UIImagePickerController alloc] init];
[ImagePicker setDelegate:self];
ImagePicker = UIImagePickerControllerSourceTypeCamera;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self ImagePicker animated:YES completion:nil];
}];
}
else
{
// turn on the camera permission from settings
}
}