iOS Swift“源类型可用”错误

ct2axkht  于 2023-01-16  发布在  Swift
关注(0)|答案(4)|浏览(199)

我还在学习Swift的过程中。我正在尝试创建一个使用相机捕捉图像的应用程序。我正在使用以下代码来查看设备中是否存在相机:

@IBAction func imageFromCamera(sender: UIBarButtonItem) {
    let imageCamera = UIImagePickerController()
    imageCamera.delegate = self

    if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
        imageCamera.sourceType = UIImagePickerControllerSourceType.Camera
    } else {
        alertUser("Camera not present")
    }
    self.presentViewController(imageCamera, animated: true, completion: nil)
}

但是,我收到错误“使用未解析的标识符”isSourceTypeAvailable“。我的代码有什么问题?
非常感谢!

inb24sb2

inb24sb21#

你可以试试这个条件在swift中检查同样的东西

if UIImagePickerController.isCameraDeviceAvailable(cameraDevice:UIImagePickerControllerCameraDevice.self)
    {

    }
bwleehnv

bwleehnv2#

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        imagePickerController.delegate = self;
        imagePickerController.modalTransitionStyle = .flipHorizontal
        imagePickerController.allowsEditing = true  
        imagePickerController.sourceType = .camera  
        imagePickerController.mediaTypes = ["public.image"]       
        imagePickerController.cameraCaptureMode = .photo        
        imagePickerController.cameraDevice = .front
        self.present(imagePickerController, animated: true) { () -> Void in
        }
 }
dw1jzc5e

dw1jzc5e3#

试试这个...可能会有帮助。

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
    {
        var imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
        imagePicker.allowsEditing = false
        imagePicker.mediaTypes = [kUTTypeImage]

        self.presentViewController(imagePicker, animated: true, completion: nil)
    }

并确保您的类也包括这两个协议。

UI图像拾取器控制器委托,UI导航控制器委托

如果您正在寻找一个很好的教程,从那里你可以学习使用相机与swift一步一步,你可以按照教程从下面的链接。
http://www.theappguruz.com/blog/user-interaction-camera-using-uiimagepickercontroller-swift

pcww981p

pcww981p4#

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
...
} else {
error
}

相关问题