UIImagePickerController以.camera而不是.savedPhotosAlbum打开

时间:2017-02-23 04:41:06

标签: ios swift camera uiimagepickercontroller gallery

我正在使用这两个单独的按钮来打开相机或图像库。 enter image description here

@IBAction func tapGal(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)

    } else {
        NSLog("No Cam Fam")
    }
}

@IBAction func tapCam(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)

    } else {
        NSLog("No Cam Fam")
    }
}

“相机”按钮正常工作,这意味着它会打开相机,但“图库”按钮也会打开相机。我已仔细检查以确保库按钮实际链接到tapGal功能。我也尝试用.savedPhotosAlbum替换.photoLibrary,但结果是一样的。

3 个答案:

答案 0 :(得分:2)

@IBAction func tapGal(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType. photoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary // use PhotoLibrary, not camera
        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)

    } else {
        NSLog("No Cam Fam")
    }
}

@IBAction func tapCam(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)

    } else {
        NSLog("No Cam Fam")
    }
}

答案 1 :(得分:1)

尝试用新方法替换您的方法。

你的错误是你已经为这两种方法传递了相同的代码行:

imagePicker.sourceType = UIImagePickerControllerSourceType.camera

的实例
imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum

将以下方法替换为您的方法: -

@IBAction func tapGal(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum



        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)

    } else {
        NSLog("No PhotoLibrary")
    }
}

答案 2 :(得分:0)

@rmaddy指出的一个简单修复。 我需要将imagePicker.sourceType = UIImagePickerControllerSourceType.camera更改为imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum