I have a question about comparing UIImages in Objective-C when one image has already been through a save and load process using the NSSearchPathForDirectoriesInDomains method.
The aim of what I want is to direct the user to a new screen upon a click depending on what the image displays.
For simplicity let's say that there are two possibilities - a black image and a green image. Clicking on the black image takes you to xib1 and clicking the green image takes you to xib2.
This is simple enough and has been working until I have implemented a save and load system.
To save I do the following:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
pngFilePath = [NSString stringWithFormat:@"%@/test.png",documentsDirectory];
data1 = [NSData dataWithData:UIImagePNGRepresentation([level1part1 objectAtIndex:0])];
[data1 writeToFile:pngFilePath atomically:YES];
and to load I do the following:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
pngFilePath = [NSString stringWithFormat:@"%@/test.png",documentsDirectory];
UIImage *image = [UIImage imageWithContentsOfFile:pngFilePath];
[button1 setImage:image forState:UIControlStateNormal];
This is fine and when I quit the program and restart it the image is retained on the screen as I wish it to be. Hypothetically let's say that image now appearing on button1 is the green image.
When I call the following code after clicking on the button with id of sender (this is button1):
if(sender.currentImage == [UIImage imageNamed:self.greenImage])
{
VisitAlreadyCorrectScreen *screen = [[VisitAlreadyCorrectScreen alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
}
even though the currentImage is the green image and is the same picture as the green image I am comparing it to, I think because I saved the green image into memory in the save process the comparison doesn't work as they are held in different places in the memory - verified by the following NSLog:
Current Image: <UIImage: 0x95614c0>, and Yes Image: <UIImage: 0xde748f0>
I cannot work out how to compare the two images so that in this case they match (they both relate to the same image I have in the resource folder). Does anyone have any suggestions at all?
Please let me know if I have not explained well enough what the problem is!
Thanks in advance!
See Question&Answers more detail:
os