To compare two UIImage
objects, you can have two ways.
- Convert both objects to
NSData
and compare using isEqual
.
- 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.