【问题标题】:Choose different images in one View with UIImagePickerController [duplicate]使用 UIImagePickerController 在一个视图中选择不同的图像 [重复]
【发布时间】:2015-12-26 15:33:50
【问题描述】:

我们有以下看法:

所有代码都运行良好,但是当我最终选择一张图片时,该照片只放在一个 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)

    }

【问题讨论】:

  • 什么是 fotoUno ?它是一个图像视图还是 4 个图像视图的属性?
  • fotoUno 是 UIImageView Mr.T 左上角的 Outlet
  • \你是在问如何在相机选择器上选择多个图像?
  • 你是要显示相机选择器4次,选择4张图片,每次选择一张图片,它都会被放入diff image view中?
  • @LeoDabus 感谢 Leo,现在一切正常

标签: ios swift uiimageview


【解决方案1】:

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

  1. 你按下 button1,然后将一个 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 个布尔值设置为 NO。

【讨论】:

  • 好的,谢谢T先生。这实际上显示了一个错误: if CamaraUno { fotoUno.image = image }
  • 我尝试这个时也是这样:如果 CamaraUno == true { fotoUno.image = image }
  • 什么样的错误?
  • 二元运算符 '==' 不能应用于 AnyObject -> () 和 bool 类型的操作数
  • 你不需要做 == true,你可以简单地做 if(camaraUno)
猜你喜欢
  • 1970-01-01
  • 2015-08-19
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
相关资源
最近更新 更多