【问题标题】:Both UIImage displaying different image [duplicate]两个 UIImage 都显示不同的图像 [重复]
【发布时间】:2018-04-24 08:23:53
【问题描述】:

我为我的 2 个 UIImage 添加了 2 个函数 UITapGestureRecognizer,用于显示每个 UIImage 的不同图片。

所以当我点击我的UIImage 之一时,UIAlertController 将显示并提供 2 个添加图片的选项(使用相机或使用库)

但是当我添加图片时,它们都显示相同的图片(当我使用imagePickerController

在我添加if else语句后,即使我点击第二个UIImage,图片也只显示在if语句上

例如:

我有 2 个 UIImage 名为 imageView1.imageimageView2.image

我点击我的第一个imageView1.imageUIAlertController 确实显示选项(相机和图书馆) 我选择库并选择 image1.jpg

我选择image1.jpg后,imageView1.imageimageView2.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。

标签: ios xcode swift4


【解决方案1】:

创建一个名为 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")
    }

我还没有测试过,但这应该可以工作。

【讨论】:

    【解决方案2】:

    试试这个代码。希望对你有用

    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")
    }
    

    【讨论】:

      【解决方案3】:

      使用这段代码在类中添加var来解决

      answer

      感谢您的回复。

      【讨论】:

      • 请详细说明解决方案。如果任何答案解决了您的问题,请将该答案标记为已接受。
      • 我正在使用这种方法link我不知道如何将这个答案标记为已接受...
      • @Renziz,您可以在答案中提供参考链接并将自己的答案标记为已接受。
      猜你喜欢
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      相关资源
      最近更新 更多