2015-12-26 24 views
0

我們有以下看法:選擇在一個視圖中不同的圖像具有的UIImagePickerController

4 different images with 4 different buttons to take a picture or select one from t

所有代碼都運行良好,但是當我終於選擇一張圖片,照片被放置只是一個ImageView的。它應該像這樣工作: 我們點擊UIImageView下面的每個按鈕,並將選定的圖片放在他的ImageView中。

我現在的問題是,所有按鈕都將照片放在第一個UIImageView中。

如何放置4張不同的照片?

class selectorDeFotosViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate { 

    //Outlets   
    @IBOutlet weak var fotoUno: UIImageView! 
    @IBOutlet weak var fotoDos: UIImageView! 
    @IBOutlet weak var fotoTres: UIImageView! 
    @IBOutlet weak var fotoCuatro: UIImageView! 

    override func viewDidLoad() { 

    } 

    //Actions  
    @IBAction func CamaraUno(sender: AnyObject) {    
     let alertPictureFrom = UIAlertController(title: "De dónde sacar la foto?", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) 

     presentViewController(alertPictureFrom, animated: true, completion: nil) 

     alertPictureFrom.popoverPresentationController?.sourceRect = CGRect(x: 350.0, y: 458.0, width: 1.0, height: 1.0) 
     alertPictureFrom.popoverPresentationController?.sourceView = self.view 

     alertPictureFrom.addAction(UIAlertAction(title: "Cámara", style: .Default, handler: {action in 

      let picker = UIImagePickerController() 

      picker.sourceType = .Camera 
      picker.delegate = self 
      picker.allowsEditing = true 

      self.presentViewController(picker, animated: true, completion: nil) 


     })) 

     alertPictureFrom.addAction(UIAlertAction(title: "Galería", style: .Default, handler: {action in 

      let picker = UIImagePickerController() 

      picker.sourceType = .PhotoLibrary 
      picker.delegate = self 
      picker.allowsEditing = true 

      self.presentViewController(picker, animated: true, completion: nil) 

     })) 
    } 

    @IBAction func camaraDos(sender: AnyObject) { 

     let alertPictureFrom = UIAlertController(title: "De dónde sacar la foto?", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) 

     presentViewController(alertPictureFrom, animated: true, completion: nil) 

     alertPictureFrom.popoverPresentationController?.sourceRect = CGRect(x: 350.0, y: 458.0, width: 1.0, height: 1.0) 
     alertPictureFrom.popoverPresentationController?.sourceView = self.view 

     alertPictureFrom.addAction(UIAlertAction(title: "Cámara", style: .Default, handler: {action in 

      let picker = UIImagePickerController() 

      picker.sourceType = .Camera 
      picker.delegate = self 
      picker.allowsEditing = true 

      self.presentViewController(picker, animated: true, completion: nil) 


     })) 

     alertPictureFrom.addAction(UIAlertAction(title: "Galería", style: .Default, handler: {action in 

      let picker = UIImagePickerController() 

      picker.sourceType = .PhotoLibrary 
      picker.delegate = self 
      picker.allowsEditing = true 

      self.presentViewController(picker, animated: true, completion: nil) 

     })) 
    } 

    @IBAction func camaraTres(sender: AnyObject) { 

     let alertPictureFrom = UIAlertController(title: "De dónde sacar la foto?", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) 

     presentViewController(alertPictureFrom, animated: true, completion: nil) 

     alertPictureFrom.popoverPresentationController?.sourceRect = CGRect(x: 350.0, y: 458.0, width: 1.0, height: 1.0) 
     alertPictureFrom.popoverPresentationController?.sourceView = self.view 

     alertPictureFrom.addAction(UIAlertAction(title: "Cámara", style: .Default, handler: {action in 

      let picker = UIImagePickerController() 

      picker.sourceType = .Camera 
      picker.delegate = self 
      picker.allowsEditing = true 

      self.presentViewController(picker, animated: true, completion: nil) 


     })) 

     alertPictureFrom.addAction(UIAlertAction(title: "Galería", style: .Default, handler: {action in 

      let picker = UIImagePickerController() 

      picker.sourceType = .PhotoLibrary 
      picker.delegate = self 
      picker.allowsEditing = true 

      self.presentViewController(picker, animated: true, completion: nil) 

     })) 
    } 

    @IBAction func camaraCuatro(sender: AnyObject) { 


     let alertPictureFrom = UIAlertController(title: "De dónde sacar la foto?", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) 

     presentViewController(alertPictureFrom, animated: true, completion: nil) 

     alertPictureFrom.popoverPresentationController?.sourceRect = CGRect(x: 350.0, y: 458.0, width: 1.0, height: 1.0) 
     alertPictureFrom.popoverPresentationController?.sourceView = self.view 

     alertPictureFrom.addAction(UIAlertAction(title: "Cámara", style: .Default, handler: {action in 

      let picker = UIImagePickerController() 

      picker.sourceType = .Camera 
      picker.delegate = self 
      picker.allowsEditing = true 

      self.presentViewController(picker, animated: true, completion: nil) 


     })) 

     alertPictureFrom.addAction(UIAlertAction(title: "Galería", style: .Default, handler: {action in 

      let picker = UIImagePickerController() 

      picker.sourceType = .PhotoLibrary 
      picker.delegate = self 
      picker.allowsEditing = true 

      self.presentViewController(picker, animated: true, completion: nil) 

     })) 



    } 


    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { 

     fotoUno.image = image 



     picker.dismissViewControllerAnimated(true, completion: nil) 

    } 
+0

什麼是fotoUno?它是一個圖像視圖還是4個圖像視圖的屬性? –

+0

fotoUno是左上方的插座UIImageView Mr.T – FactorJose

+0

\你在問如何在相機選擇器上選擇多個圖像? –

回答

0

有很多方法可以做到這一點。

  1. 按按鈕1,和一個BOOL值buttonOne設置爲YES,並且您目前的選擇器,選擇該圖像,並顯示該圖像,則只需查看被按鈕在方法didFinishPickingImage選中或不,所以:

     if(buttonOne) 
         { 
           //set the image view 1 image 
         } 
    

你爲所有四個圖像視圖做同樣的:

   if(buttonOne) 
      { 
        //set the image view 1 image 
      } 
      if(buttonTwo) 
      { 
        //set the image view 2 image 
      } if(buttonThree) 
      { 
        //set the image view 3 image 
      } if(buttonFour) 
      { 
        //set the image view 4 image 
      } 

確保在將其中一個值設置爲YES時,將其他3個BOOL值設置爲NO。

+0

好的,謝謝Mr.T。 如果CamaraUno { fotoUno.image =圖像 } – FactorJose

+0

同樣的,當我試試這個::這實際上顯示錯誤 如果CamaraUno ==真{ fotoUno.image =圖像 } – FactorJose

+0

什麼樣的錯誤? –

相關問題