gpt4 book ai didi

ios - 使用 UIImagePickerController 在一个 View 中选择不同的图像

转载 作者:行者123 更新时间:2023-11-30 13:49:28 25 4
gpt4 key购买 nike

我们有以下观点:

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. 您按下button1,并将BOOL值buttonOne设置为YES,然后您显示选择器,选择图像,并显示图像,您只需在方法中检查按钮是否被选中didFinishPickingImage,所以:

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

您对所有四个 ImageView 执行相同的操作:

             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。

关于ios - 使用 UIImagePickerController 在一个 View 中选择不同的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com