1
2012-11-21 11:54:50.607 [10152:907] Current   Image: <UIImage: 0x20819ae0>
2012-11-21 11:54:50.608 [10152:907] EmptyMark Image: <UIImage: 0x20819ae0>

After App Enters the background and comes back to foreground.

2012-11-21 11:54:57.040 [10152:907] Current   Image: <UIImage: 0x20819ae0>
2012-11-21 11:54:57.042 [10152:907] EmptyMark Image: <UIImage: 0x208cc580>

I'm comparing the same image "emptymark.png" it is added to a button "Current Image" (logged) to the actual file "EmptyMark Image" (Logged) when I come back to the app after it has been in the background EmptyMark returns different from "Current Image" (the image on the button).

I'm getting "EmptyMark Image" using [UIImage imageNamed:@"emptymark.png"]

Should I be holding a reference to emptymark.png all the time rather then trying to compare from the file?

KDM
  • 197
  • 5
  • 18
  • Found this SO question: same issue, as I'm sure you know, is equal is not a great way to do the checking because the memory addresses change. http://stackoverflow.com/questions/11216167/objective-c-comparing-an-image-against-another-image-that-has-been-previously-s – KDM Nov 21 '12 at 20:16
  • I dont think the solution suggested is good in this case. Using `if ([UIImagePNGRepresentation(blackImage) isEqualToData:UIImagePNGRepresentation(greenImage)])' has to create NSData from both images and then has to compare which is not good for app performance if the images are larger. – iDev Nov 21 '12 at 20:20

1 Answers1

1

You can hold the reference to UIImage as,

self.image = [UIImage imageNamed:@"emptymark.png"];

When you are comparing using just [UIImage imageNamed:@"emptymark.png"], it is creating separate objects.

iDev
  • 23,310
  • 7
  • 60
  • 85
  • 1
    This is what I ended up doing. The above referenced link works however the ([UIImagePNGRepresentation(blackImage) isEqualToData:UIImagePNGRepresentation(greenImage)])is far from elegant. – KDM Nov 21 '12 at 21:10