gpt4 book ai didi

swift - 如何以相同的方法使用不同的按钮来获得不同的结果?

转载 作者:行者123 更新时间:2023-11-28 07:33:50 26 4
gpt4 key购买 nike

我有一个由四张不同图片组成的正方形,两张在顶部,两张在底部,要在从图片库加载后显示。

每个 ImageView 都与其个人按钮相关联,该按钮将使用与进入用户库以获取图像相同的方法。

如何用相同的方法使用四个不同的按钮将加载的图像显示在适当的位置?

我可以制作一个模型来执行此行为吗?

谢谢

@IBOutlet weak var topLeftImage: UIImageView!
@IBOutlet weak var topRightImage: UIImageView!
@IBOutlet weak var bottomLeftImage: UIImageView!
@IBOutlet weak var bottomRightImage: UIImageView!

@IBOutlet weak var topLeftButton: UIButton!
@IBOutlet weak var topRightButtom: UIButton!
@IBOutlet weak var bottomLeftButton: UIButton!
@IBOutlet weak var bottomRightButton: UIButton!

@IBAction func loadTopLeftImage(_ sender: Any) {
loadPhotoLibrary()
}

@IBAction func loadTopRightImage(_ sender: Any) {
loadPhotoLibrary()
}

@IBAction func loadBottomLeftImage(_ sender: Any) {
loadPhotoLibrary()
}

@IBAction func loadBottomRightImage(_ sender: Any) {
loadPhotoLibrary()
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {


topLeftImage.image = image

}
self.dismiss(animated: true, completion: nil)
}


func loadPhotoLibrary() {

imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
imagePicker.allowsEditing = false

self.present(imagePicker, animated: true)
}

最佳答案

首先创建UIImagePickerController的子类,并添加变量completionHandler,参数类型为UIImage?

class YourPicker: UIImagePickerController {
var completionHandler: ((UIImage?) -> ())?
}

然后将参数添加到您的 loadPhotoLibrary 函数,它将代表 UIImageView 您想要更改其 image。同时设置它的完成处理程序:你想改变某些 ImageView 的 image

func loadPhotoLibrary(setImageView imageView: UIImageView) {

let imagePicker = YourPicker() // don't forget to initialize subclass

imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
imagePicker.allowsEditing = false

imagePicker.completionHandler = { image in
if let image = image { // if you just dismiss picker, you don't want to delete previous image
imageView.image = image
}
}

self.present(imagePicker, animated: true)
}

现在在 picker 的委托(delegate)方法中将 picker 向下转换为 YourPicker 并使用转换为 UIImage 的键 UIImagePickerController.InfoKey 的值调用完成? (如果它的值为nil,无所谓,只是不设置ImageView的image)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

if let myPicker = picker as! YourPicker {
myPicker.completionHandler!(info[UIImagePickerController.InfoKey.originalImage] as? UIImage)
}
self.dismiss(animated: true, completion: nil)
}

然后就在按钮的操作中像这样调用你的loadPhotoLibrary方法

@IBAction func loadTopLeftImage(_ sender: Any) {
loadPhotoLibrary(setImageView: topLeftImage)
}
...

关于swift - 如何以相同的方法使用不同的按钮来获得不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53837484/

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