简体   繁体   English

您可以在一个视图控制器上使用两个不同的相机按钮来显示两个不同的图像吗? (快速3)

[英]Can you have two different camera buttons for two different image views on one view controller? (Swift 3)

Here is my code below: Basically I am trying to give the user an option to choose the camera button1 or choose from photo library button1. 这是下面的代码:基本上,我试图为用户提供一个选择相机按钮1或从照片库按钮1中进行选择的选项。 Then depending on the button they press it adds to imageview1. 然后,根据他们按下的按钮,将其添加到imageview1。 Then on the same view controller it is duplicated on the bottom half of the display with a camera button2 and a photo library button 2 that will go to the imageview2. 然后,在同一视图控制器上,将其复制到显示屏的下半部分,并带有将进入imageview2的相机按钮2和照片库按钮2。

    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)

        }
    }
}

The problem is your use of two different variables to track which image view should be updated. 问题是您使用两个不同的变量来跟踪应更新哪个图像视图。 Either reset both variables after updating an image view or use only one variable. 更新图像视图后重置两个变量,或仅使用一个变量。

And there is no need for two UIImagePickerController instances. 并且不需要两个UIImagePickerController实例。

Lastly, update the tags of all 4 buttons. 最后,更新所有4个按钮的标签。 For the two buttons that affect ImageView1 , set their tag to 1. For the two buttons that affect ImageView2 , set their tag to 2. Never use a tag of 0 since all views have a default tag of 0 . 对于影响两个按钮ImageView1 ,设置它们的标签1.对于影响两个按钮ImageView2 ,设置它们的标签2.请勿使用的标签0 ,因为所有的观点有一个默认标签0

BTW - name variables and functions with a leading lowercase letter. BTW-用前导小写字母命名变量和函数。 Only class and struct names should start with uppercase. 只有类和结构名才能以大写字母开头。

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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不同视图控制器中的两个表视图一个工作一个不工作(Swift 4) - Two Table Views In Different View Controllers one working and one not(Swift 4) 一个视图控制器中具有不同重用单元格的两个集合视图 - Two Collection Views with Different Reuse Cells in One View Controller 我们可以为一个View Controller使用两个不同的Xib吗? - Can we use Two different Xib's for one View Controller? 有两个按钮打开两个不同的视图控制器 - Have two buttons open two different view controllers Swift 2.0在一个视图控制器cellForRowIndexPath中包含两个表视图 - Swift 2.0 two table views in one view controller cellForRowIndexPath Swift 4 One View Controller两个视图隐藏导航栏 - Swift 4 One View Controller two views hide navigation bar 有2个容器视图。 需要将一个更改为其他视图控制器 - Have 2 container views. Need to change one to a different view controller 处理具有两种不同视图的视图控制器的最佳方法:纵向和横向 - The best way to handle a view controller with two different views: portrait and landscape 两个不同的按钮对同一个视图控制器执行转场,但希望通过一个按钮禁用转场的用户交互 - Two different buttons performing segue to same view controller but want to disable user interaction for segue through one button swift:同一视图控制器中视图的不同对齐方式 - swift:Different alignment of views in same view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM