0

In my scenario total 9 images in a UIView. In these 9 images 2 or 3 may be same, So when I drag one ImageView and drop it into another imageView ,so after dropping I wanna compare the drag image with that image where I drop my drag image.

Hope you will understand my question easily. Thank you in advance!

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43

2 Answers2

1

To compare two UIImage objects, you can have two ways.

  1. Convert both objects to NSData and compare using isEqual.
  2. Convert both objects to Data and compare using elementsEqual.

The latter is apparently more accurate. Check more information/discussion here: How to compare two UIImage objects

extension UIImage {
    func isEqual(to image: UIImage) -> Bool {
        guard let data1: Data = UIImagePNGRepresentation(self),
            let data2: Data = UIImagePNGRepresentation(image) else {
                return false
        }
        return data1.elementsEqual(data2)
    }
}

I hope this helps.

EDIT:

to do something like this https://www.youtube.com/watch?v=6zdgFwAE2y4 (from OP's comment), there are a couple of ways to do that. One that I can think of is you can make a class with properties like UIImage and identifier or say shape.

In that way, you can easily compare two different images/objects by the custom property shape. :) Good luck.

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95
  • youtube.com/watch?v=6zdgFwAE2y4 Basically I'm working on this game. Hope you can understand my question after watching this video. – Fareed Afzal Jan 15 '19 at 09:33
  • @FareedAfzal See my edit. You can easily do that. – Glenn Posadas Jan 15 '19 at 09:38
  • Can you please provide me the complete source code for one object. In the video they drag many images, but can you give me a source code for only one object. That one object easily draggable and put it into right location. Thanks! – Fareed Afzal Jan 15 '19 at 10:27
0

Although you can not drop one imageView to other but you can Put One ImageView over the other for example you can put ImgView1 on or above ImageView2 and you can compare them by giving them different background images or you can click on the View controller scene and it will indicate which UIelement you are clicking on

You can do:---`func image(_ image1: UIImage?, isEqualTo image2: UIImage?) -> Bool { let data1: Data? = UIImagePNGRepresentation(image1) let data2: Data? = UIImagePNGRepresentation(image2)

return data1 == data2

} `

Nishant Pathania
  • 349
  • 1
  • 14
  • https://www.youtube.com/watch?v=6zdgFwAE2y4 Basically I'm working on this game. Hope you can understand my question after watching this video. – Fareed Afzal Jan 15 '19 at 09:31
  • In That case You can do. func image(_ image1: UIImage?, isEqualTo image2: UIImage?) -> Bool { let data1: Data? = UIImagePNGRepresentation(image1) let data2: Data? = UIImagePNGRepresentation(image2) return data1 == data2 } – Nishant Pathania Jan 15 '19 at 09:46
  • can you please provide me to the complete source code for one object. In the video they drag many images, but can you give me a source code for only one object. This one object easily draggable and put it into right location. Thanks! – Fareed Afzal Jan 15 '19 at 10:27
  • fareedrao7890@gmail.com It's my personal email. You can send the source code on it. – Fareed Afzal Jan 15 '19 at 10:30