【发布时间】:2018-04-24 08:23:53
【问题描述】:
我为我的 2 个 UIImage 添加了 2 个函数 UITapGestureRecognizer,用于显示每个 UIImage 的不同图片。
所以当我点击我的UIImage 之一时,UIAlertController 将显示并提供 2 个添加图片的选项(使用相机或使用库)
但是当我添加图片时,它们都显示相同的图片(当我使用imagePickerController时
在我添加if else语句后,即使我点击第二个UIImage,图片也只显示在if语句上
例如:
我有 2 个 UIImage 名为 imageView1.image 和 imageView2.image
我点击我的第一个imageView1.image,UIAlertController 确实显示选项(相机和图书馆)
我选择库并选择 image1.jpg
我选择image1.jpg后,imageView1.image和imageView2.image都显示同一张图片。
在func imagePickerController 中添加if else 语句后,image1.jpg 仅显示在if 语句中...即使我点击imageVIew2.image
但是,我想要的是……
当我点击 imageView1.image 并选择 image1.jpg 时,imageView1.image 显示 image1.jpg 和 imageView2.jpg 不显示任何内容。
这是我的代码(没有 if else):
extension KUYPEP {
//pencetGambarnya1 for imageView1.image
@objc func pencetGambarnya1(tapGestureRecognizer1: UITapGestureRecognizer) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let gambarDipencet1 = tapGestureRecognizer1.view as! UIImageView
let act1 = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
act1.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera;
self.present (imagePickerController, animated: true, completion: nil)
} else {
print ("Camera ruksak")
}
}))
act1.addAction(UIAlertAction(title: "Library", style: .default, handler: {(UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
act1.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {(UIAlertAction)in}))
self.present(act1, animated: true, completion: nil)
print ("berhasil11")
}
//pencetGambarnya2 for imageView2.image
@objc func pencetGambarnya2(tapGestureRecognizer2: UITapGestureRecognizer) {
let imagePickerController2 = UIImagePickerController()
imagePickerController2.delegate = self
let gambarDipencet2 = tapGestureRecognizer2.view as! UIImageView
let act2 = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
act2.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController2.sourceType = .camera;
self.present (imagePickerController2, animated: true, completion: nil)
} else {
print ("Camera ruksak")
}
}))
act2.addAction(UIAlertAction(title: "Library", style: .default, handler: {(UIAlertAction) in
imagePickerController2.sourceType = .photoLibrary
self.present(imagePickerController2, animated: true, completion: nil)
}))
act2.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {(UIAlertAction)in}))
self.present(act2, animated: true, completion: nil)
print ("berhasil22")
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image1 = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView1.image = image1
let image2 = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView2.image = image2
picker.dismiss(animated: true, completion: nil)
print ("Slavic Hardbass")
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
这是我的viewDidLoad
let tapGestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(pencetGambarnya1(tapGestureRecognizer1:)))
imageView1.isUserInteractionEnabled = true
imageView1.addGestureRecognizer(tapGestureRecognizer1)
let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(pencetGambarnya2(tapGestureRecognizer2:)))
imageView2.isUserInteractionEnabled = true
imageView2.addGestureRecognizer(tapGestureRecognizer2)
【问题讨论】:
-
在
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])中,您将两个图像视图都设置为选定图像。所以当然两者都具有与您的代码相同的图像 -
我只是在调试的时候才意识到,但是我不知道如何设置每个使用if else。