如何限制用户在iOS应用中使用后置摄像头

时间:2016-07-21 01:22:14

标签: ios swift xcode cocoa-touch uiimagepickercontroller

我正在开发一款iOS应用,用户可以在某一时刻拍照。但是,我希望用户只能使用前置摄像头。也就是说,他们不应该使用后置摄像头。目前,我的代码如下所示:

@IBAction func takePicture(sender: UIButton) {

    if UIImagePickerController.availableCaptureModesForCameraDevice(.Front) != nil {
        let imagePickerController = UIImagePickerController()
        imagePickerController.sourceType = .Camera
        imagePickerController.allowsEditing = false
        imagePickerController.cameraDevice = .Front
        imagePickerController.delegate = self
        presentViewController(imagePickerController, animated: true, completion: nil)
    }
    else {
        let alertVC = UIAlertController(
            title: "No Camera",
            message: "Sorry, this device has no camera",
            preferredStyle: .Alert)
        let okAction = UIAlertAction(
            title: "OK",
            style:.Default,
            handler: nil)
        alertVC.addAction(okAction)
        presentViewController(alertVC,
                              animated: true,
                              completion: nil)
    }
}

虽然这默认将相机设为正面,但它仍然为用户提供了切换到后置摄像头的选择。有没有办法限制这个功能?

提前致谢

1 个答案:

答案 0 :(得分:0)

如果您不想让用户获得UIImagePickerController提供的自由,请不要使用UIImagePickerController。设计自己的拍照界面(通过AVFoundation),完全可以控制。