UIImagePickerController使用Photo / Retake按钮不起作用

时间:2017-05-30 14:06:54

标签: ios swift

我使用默认的UIImagePickerController拍摄照片/从库中选择它们。在某些时候,我通过UIImagePickerController呈现其他ViewController,用于自定义所选照片。如果用户选择“取消”选项,我想让他回到图像选择屏幕 - 在使用“使用照片/重新拍摄”按钮的相机源屏幕的情况下。在解除使用自定义按钮的ViewController后,它们不起作用(它们之前有效)。

我不想在这里使用AVFoundation。

以下是一些相关代码:

let storyBuilder = StoryBuilderViewController.storyboardController()

storyBuilder.delegate = self

photoPickerController.present(storyBuilder, animated: true)

这样我就会展示我的自定义屏幕。

func resetButtonTapped(_ storyBuilder: StoryBuilderViewController) {
    storyBuilder.dismiss(animated: true)
}

此代码在解雇后运行。

所以,没什么特别的,但按钮停止工作

查看层次结构如下所示:

View Hierarchy

所以,这个屏幕上什么都没有。按钮属性isEnabled设置为true。 UIImagePickerController不是零。 isUserInteractionEnabled为true

我创建了一个simple project,但出现此错误。它也不起作用。 此外,我刚刚遇到一个案例,当相机完全停止工作时,即使在应用程序重启后,日志也会出现错误。只有重新启动手机才有帮助。

更多说明

我有一个开始屏幕 - RootViewController。我需要在它上面放一个带摄像头的UIImagePickerController。用户拍照后我需要提供另一个控制器 - EditViewController(不解雇UIImagePickerController,所以如果用户想拍另一张照片 - 他就回去了)。我是这样做的:

picker(editController, animated: true) //picker - UIImagePickerController, editController - edit controller

正如我之前所说,我的用户可以回去 - 这是一个问题 - 如果我解除editController,UIImagePickerController上的按钮不起作用。

请问一些问题,我不明白,为什么很难正确地解决我的问题。

2 个答案:

答案 0 :(得分:5)

我已经检查了您的演示代码并进行了一些更改,以使其按您的需要运行。 1)请检查ViewController类代码。

class ViewController: UIViewController {
    fileprivate var photoPickerController = UIImagePickerController()
    let notificationIdentifier: String = "StartCamera"

    override func viewDidLoad() {
        // Register to receive notification
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.cameraSetUp), name: NSNotification.Name(rawValue: notificationIdentifier), object: nil)
    }

    func cameraSetUp(){
        photoPickerController.sourceType = .camera
        photoPickerController.cameraDevice = .front
        photoPickerController.delegate = self
        present(photoPickerController, animated: true)

    }
    @IBAction func showCamera(_ sender: Any) {
        cameraSetUp()
    }
}

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard let storyboard = storyboard else {return}
        let viewController = storyboard.instantiateViewController(withIdentifier: "EditingViewController")
        picker.dismiss(animated: true)
        present(viewController, animated: true)
    }

    public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true)
    }
}

2)EditingViewController类代码

class EditingViewController: UIViewController {
    let notificationIdentifier: String = "StartCamera"

    @IBAction func dismissButtonTapped(_ sender: Any) {
        // Post a notification
        dismiss(animated: true)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: notificationIdentifier), object: nil)
    }
}

答案 1 :(得分:1)

所以你的问题是你正在堆叠controllers。你不需要这样做。另一件事是cancel button中的viewController没有用处。您还需要保留UIImagePickerController的引用,使其成为视图控制器的实例属性并显示它。

以下代码:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    let photoPickerController = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        photoPickerController.delegate = self
    }


    @IBAction func cancelBtnTapped(_ sender: UIButton) {
        showFrontCamera()
    }

    @IBAction func showCamera(_ sender: Any) {
        showFrontCamera()
    }

    func showFrontCamera() {
        photoPickerController.sourceType = .camera
        photoPickerController.cameraDevice = .front
        present(photoPickerController, animated: true)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print("didFinishPickingMediaWithInfo")
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            // your image
        }

        picker.dismiss(animated: true){
            // any code after dismissing the controller
        }
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print("imagePickerControllerDidCancel")
        picker.dismiss(animated: true){
            // any code after dismissing the controller
        }
    }
}