2

I'm trying to create a conditional that asks about the button's image. Here's what I have:

    if (buttonName.image == [UIImage imageNamed:@"ButtonImage.png"])

This code works fine if the object buttonName is a UIImageView, but as a UIButton, I get the error 'request for member 'image' in something not a structure or union'.

How can I create this conditional?

user813031
  • 61
  • 5

1 Answers1

1

== checks for pointer equality, so generally I would recommend using isEqual: which works on NSObjects, but this may not work for a UIImage (though UIImage does inherit from NSObject so give it a go).

If that fails, you might try using the CGImage property of a UIImage. This is the underlying Quartz image data, so you may be able to detect if the two images have the same CGImage (again, using == will not work, but isEqual may).

One way around this is to use the tag property of the UIButton, but this takes only ints, so you'll have to do some re-writing.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • Also, this post is related: [Cocoa Touch - Comparing Images](http://stackoverflow.com/questions/3400707/cocoa-touch-comparing-images). – PengOne Jun 23 '11 at 20:44
  • Comparing the data of two arbitrary images is a reasonably expensive operation; it would be surprising if that was what `isEqual:` did. I'd guess that it's just using the default `NSObject` protocol implementation, which compares the two objects' `hash`es. – jscs Jun 23 '11 at 21:14
  • @Josh: Yes, I think comparing pixel for pixel is expensive and probably not worth it. I don't know if `isEqual` will actually work, though. I'd be interested to hear back. – PengOne Jun 23 '11 at 21:18
  • I actually think I have solved the problem by changing buttonName.image to buttonName.currentImage – user813031 Jun 24 '11 at 02:07