2017-03-17 72 views
0

這裏是我的代碼如下:基本上我試圖讓用戶選擇相機button1或從照片庫button1中選擇一個選項。然後根據按鈕他們按下它添加到imageview1。然後在相同的視圖控制器上,它被複制到顯示器的下半部分,並帶有一個照相機按鈕2和一個照片庫按鈕2,它們將轉到imageview2。在一個視圖控制器上,您是否有兩個不同的相機按鈕可用於兩個不同的圖像視圖? (Swift 3)

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) 

     } 
    } 
} 
+0

由於您發佈的代碼似乎通常會按照您的要求進行操作,您的問題或問題到底是什麼? – rmaddy

+0

由於某種原因,當我按下相機按鈕2並拍攝照片時,它會填充兩個圖像視圖。圖像似乎在兩個圖像視圖之間跳轉,每次使用相機按鈕時都會更改。 – FreitDev

回答

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

非常感謝您的幫助@rmaddy。反饋對我來說非常重要,因爲我是一名新開發人員,現在我知道我將來的項目。現在它適用於我。 – FreitDev

相關問題