使用UIImagePickerController在一个视图中选择不同的图像

时间:2015-12-26 15:33:50

标签: ios swift uiimageview

我们有以下观点:

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)

    }

1 个答案:

答案 0 :(得分:0)

有很多方法可以实现这一目标。

  1. 按下button1,并将BOOL值buttonOne设置为YES,然后显示选择器,选择图像,并显示图像,只需检查方法中是否选择了按钮{{ 1}},因此:

    didFinishPickingImage
  2. 您对所有四个图像视图都执行相同的操作:

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

    当您将一个值设置为YES时,请确保将其他3个bool值设置为NO。