UIImagePickerController 异常:“源类型必须是 UIImagePickerControllerSourceTypeCamera”

问题描述 投票:0回答:6

这有什么问题吗?我真的不明白 UIImagePickerController 的一些重要部分......

来源:

     UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;        
imagePickerController.delegate = self;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];

我无法打开照片库吗? 任何帮助表示赞赏!

iphone uiimagepickercontroller
6个回答
39
投票

如果您使用的是照片库,是否需要设置

cameraCaptureMode


15
投票

也许不是你的情况,但当你初始化 UIImagePickerController 对象时,首先需要在设置任何其他属性之前设置源类型,否则你会得到相同的错误。

let imagePickerController =  UIImagePickerController()
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
imagePickerController.cameraCaptureMode =   
UIImagePickerControllerCameraCaptureMode.Photo
imagePickerController.cameraDevice = .Front
imagePickerController.showsCameraControls = true;
imagePickerController.navigationBarHidden = false;
imagePickerController.toolbarHidden = false;
imagePickerController.delegate = self

10
投票

我的问题是我在设置

.front
之前将相机设备设置为
imagePicker.sourceType = .camera
,显然它导致了崩溃。 所以我改变了我的代码:

 imagePicker.cameraDevice = .front
 imagePicker.sourceType = .camera

对此:

imagePicker.sourceType = .camera
imagePicker.cameraDevice = .front

问题解决了:)


7
投票

我遇到了完全相同的异常,但是我没有使用 UIImagePickerController 控件的实例,而是在 UIWebView 内显示一个网页,该网页具有带有 HTML 媒体访问属性的输入以启用相机访问。

问题在于 UIWebView 似乎无法正确处理 HTML 媒体访问属性。我注意到这些属性的规范随着时间的推移发生了几次变化,因此虽然它看起来像有效的 html 并且可以在设备上的 safari 上运行,但它不适用于使用 UIWebView 呈现的页面。

坏:

<input type="file" accept="image/*" capture="camera" />

好:

<input type="file" accept="image/*;capture=camera" />

异常立即消失并拍照。这对于 iOS 10 或 11 来说似乎是新的,因为它是现有代码并且过去没有爆炸。


3
投票
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {

            imagePickerController.cameraDevice =  UIImagePickerControllerCameraDeviceFront;
        } else {
            imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        }

    [self presentModalViewController:imagePickerController animated:YES];
    [imagePickerController release];
}
else {
    // do stuff ///....Alert 
}

0
投票

如果您在 swiftUI View 上使用 UIImagePickerController,请使用 @State 设置您的 UIImagePickerController 。 示例:

import SwiftUI

struct SimpleView: View {
@State var cameraPicker:UIImagePickerController = UIImagePickerController()
    var body: some View {
         CameraView(picker:cameraPicker)
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.