在一个视图控制器上,您可以为两个不同的图像视图配备两个不同的相机按钮(斯威夫特3)

时间:2017-03-17 20:50:05

标签: ios

以下是我的代码:基本上我试图让用户选择相机按钮1或从照片库按钮1中选择。然后根据按钮按下它添加到imageview1。然后在同一个视图控制器上,它将复制在显示屏的下半部分,带有一个摄像头按钮2和一个将进入imageview2的照片库按钮。

    class ViewController: UIViewController, UIImagePickerControllerDelegate,    UINavigationControllerDelegate, UITextFieldDelegate {

    @IBOutlet weak var ImageView1: UIImageView!
    @IBOutlet weak var ImageView2: UIImageView!
    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!

var imagePicker = UIImagePickerController()
    var cameraImagePicker = UIImagePickerController()
    var imagePicked = 0
    var cameraPicked = 0

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = false
        cameraImagePicker.delegate = self
        cameraImagePicker.sourceType = .camera
        cameraImagePicker.allowsEditing = false
        textField1.delegate = self
        textField2.delegate = self
}

  @IBAction func uploadImageBtnClick1(_ sender: UIButton) {

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
            print("Working")

            imagePicked = sender.tag

            self.present(imagePicker, animated: true, completion: nil)
        }
    }
  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

            if imagePicked == 0{
                ImageView1.image = pickedImage
            } else if imagePicked == 1{
                ImageView2.image = pickedImage
            } else {

                print("Something went wrong")
            }
            if cameraPicked == 2 {
                ImageView1.image = pickedImage
            } else if cameraPicked == 3 {
                ImageView2.image = pickedImage
            }
        } else {

            print("Something went wrong")
        }
        dismiss(animated: true, completion: nil)
    }
   @IBAction func CameraButtonTapped(_ sender: UIButton) {

         cameraPicked = sender.tag

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            print("working")


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

        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是您使用两个不同的变量来跟踪应更新的图像视图。在更新图像视图后重置两个变量或仅使用一个变量。

并且不需要两个UIImagePickerController个实例。

最后,更新所有4个按钮的标签。对于影响ImageView1的两个按钮,请将其标记设置为1.对于影响ImageView2的两个按钮,将其标记设置为2.永远不要使用0的标记,因为所有视图都有默认标记0

BTW - 使用前导小写字母命名变量和函数。只有类和结构名称应以大写字母开头。

class ViewController: UIViewController, UIImagePickerControllerDelegate,    UINavigationControllerDelegate, UITextFieldDelegate {
    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!
    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!

    var imagePicked = 0

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

    @IBAction func uploadImageBtnClick1(_ sender: UIButton) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
            print("Working")

            imagePicked = sender.tag

            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary
            imagePicker.allowsEditing = false

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

    @IBAction func cameraButtonTapped(_ sender: UIButton) {
         if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            print("working")

            imagePicked = sender.tag

            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .camera
            imagePicker.allowsEditing = false

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

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            if imagePicked == 1 {
                imageView1.image = pickedImage
            } else if imagePicked == 2 {
                imageView2.image = pickedImage
            } else {
                print("Something went wrong")
            }
        } else {
            print("Something went wrong")
        }
        dismiss(animated: true, completion: nil)
    }
}