为什么我的UIImageView取代了第二个?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我的UIImageView取代了第二个?相关的知识,希望对你有一定的参考价值。
我有两个不同的UIImageViews的插座,当我选择第一个它将出现在第一个图像视图时,但当我选择第二个图像时,它取代了第一个图像视图,即使它连接到第二个ImageView。这是我选择图像按钮的代码。
@IBOutlet weak var myImageView1: UIImageView!
@IBOutlet weak var myImageView2: UIImageView!
@IBAction func pickImage1(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image, animated: true)
}
//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView1.image = image
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
@IBAction func pickImage2(_ sender: Any) {
let image2 = UIImagePickerController()
image2.delegate = self
image2.sourceType = UIImagePickerControllerSourceType.photoLibrary
image2.allowsEditing = false
self.present(image2, animated: true)
}
//Add didFinishPickingMediaWithInfo here
func imagePickerController2(_ picker2: UIImagePickerController, didFinishPickingMediaWithInfo2 info2: [String : Any]) {
if let image2 = info2[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView2.image = image2
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
答案
试试这个代码。因此,您需要一个标记来记住单击哪个图像视图,然后根据该标记设置图像。
var selected = 1
@IBAction func pickImage1(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
selected = 1
self.present(image, animated: true)
}
//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
if selected == 1 {
myImageView1.image = image
} else {
myImageView2.image = image
}
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
@IBAction func pickImage2(_ sender: Any) {
let image2 = UIImagePickerController()
image2.delegate = self
image2.sourceType = UIImagePickerControllerSourceType.photoLibrary
image2.allowsEditing = false
selected = 2
self.present(image2, animated: true)
}
继续前进,当您有多个图像视图时,可以使用另一种方法来避免在任何地方复制代码。
首先,为每个图像视图添加唯一标记。避免使用0,因为默认标记为0.因此,您将拥有标记为1到4的图像视图。
对所有图像视图调用相同的方法,以便通过单击其中任何一个来触发此功能
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.addGestureRecognizer(tapGestureRecognizer)
处理程序看起来像这样
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
let tappedImage = tapGestureRecognizer.view as! UIImageView
selected = tappedImage.tag
self.present(image, animated: true)
}
最后在图片选择委托
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
if let imageView = self.view.viewWithTag(selected) as? UIImageView {
imageView.image = image
}
}
else {
//error
}
self.dismiss(animated: true, completion: nil)
}
另一答案
问题是您已重命名委托方法。如果这样做,将无法识别或调用该方法。
选定答案的另一个选择是扩展UIImageView
并使其遵守UIImagePickerControllerDelegate
/ UINavigationControllerDelegate
。
extension UIImageView: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
//handle error
return
}
image = selectedImage
picker.presentingViewController?.dismiss(animated: true)
}
func presentImagePicker(from viewController: UIViewController) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
picker.allowsEditing = false
viewController.present(picker, animated: true)
}
}
这很好,因为您可以使用一行为应用程序中的任何UIImageView
启动图像选择器,如下所示:
@IBAction func pickImage1(_ sender: UIButton) {
myImageView1.presentImagePicker(from: self)
}
以上是关于为什么我的UIImageView取代了第二个?的主要内容,如果未能解决你的问题,请参考以下文章
XCode iPhone 多个目标 - 创建了第二个不同名称的可执行文件,但我的第二个“可执行文件”的“路径”是错误的
为啥我的 cocos2d box2d 项目中我的第二个球没有弹跳?