我为我的2 UITapGestureRecognizer
添加了2个函数UIImage
,用于显示每个UIImage
的不同图片。
因此,当我点按其中一个UIImage
时,UIAlertController
会显示并提供2个添加图片的选项(使用相机或使用图书馆)
但是当我添加图片时,它们都显示相同的图片(当我使用imagePickerController
时
在我添加if else
语句后,即使我点击了第二个if
UIImage
语句中
例如:
我有2 UIImage
名为imageView1.image
和imageView2.image
点击我的第一个imageView1.image
,UIAlertController
显示选项(相机和库)
我选择了库并选择了image1.jpg
选择image1.jpg后,imageView1.image
和imageView2.image
都显示相同的图片。
我在if else
中添加func imagePickerController
语句后,image1.jpg仅显示在if
语句中...即使我点按了imageVIew2.image
但是,我想要的是......
当我点按imageView1.image
并选择image1.jpg时,显示image1.jpg的imageView1.image
和imageView2.jpg
不显示任何内容。
这是我的代码(没有其他):
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)
答案 0 :(得分:0)
创建一个名为targetImage的公共UIImage并执行以下操作, 在呈现imagePickerController之前,将image1或image2的引用分配给targetImage。完成拾取图像后,只需将拾取的图像设置为targetImage。你可以在下面的代码中看到,我已经设置了targetImage = imageView1。
act1.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera;
targetImage = imageView1
self.present (imagePickerController, animated: true, completion: nil)
} else {
print ("Camera ruksak")
}
}))
对imageView2也一样, 之后修改代码以设置这样的选择媒体,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image1 = info[UIImagePickerControllerOriginalImage] as! UIImage
targetImage.image = image1
picker.dismiss(animated: true, completion: nil)
print ("Slavic Hardbass")
}
我没有对此进行测试,但这应该可行。
答案 1 :(得分:0)
试试此代码。希望它适合你
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if picker == imagePickerController
{
let image1 = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView1.image = image1
}
else
{
let image2 = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView2.image = image2
}
picker.dismiss(animated: true, completion: nil)
print ("Slavic Hardbass")
}
答案 2 :(得分:0)