I have a question about comparing UIImages in Objective-C when a single image has already gone through the save and load process using the NSSearchPathForDirectoriesInDomains method.
The goal of what I want is to direct the user to a new screen with a click, depending on what the image displays.
For simplicity, we say that there are two possibilities - a black image and a green image. Clicking on the black image will lead you to xib1, and clicking on the green image will lead you to xib2.
It is quite simple and works 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 for download 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 normal, and when I exit the program and restart it, the image is saved on the screen as I would like. Hypothetically suppose that the image that appears on button 1 is a green image.
When I call the following code after clicking the button with the sender ID (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]; }
although currentImage is a green image and is the same image as the green image on which I am comparing it, I think because I saved the green image in memory during saving, the comparison does not work, because they are held in different places in the memory - verified by the following NSLog:
Current Image: <UIImage: 0x95614c0>, and Yes Image: <UIImage: 0xde748f0>
I canβt figure out how to compare the two images so that in this case they match (both refer to the same image that I have in the resources folder). Does anyone have any suggestions?
Please let me know if I have not explained well enough what the problem is!
Thanks in advance!