Image comparison with button image - ios

I am trying to compare an image from a button with another image but its not working as expected and as stackoverflow suggestions...
if (favoriteButton.imageView.image == [UIImage imageNamed:#"favorite.png"]) {
NSLog(#"yes!");
}
anything else I can do? I tried to get the image file name but it seems to be impossible.

UIImage is a class, not a native type. You should use the isEqual: method, not the == operator to see if the two objects represent the same data.
if ([favoriteButton.imageView.image isEqual:[UIImage imageNamed:#"favorite.png"]]) {
NSLog(#"yes!");
}
Only use the == operator with object pointers if you really want to see if the two pointer reference the exact same location in memory.

I found this: How to compare two UIImage objects
this is what I tink may help:
- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
NSData *data1 = UIImagePNGRepresentation(image1);
NSData *data2 = UIImagePNGRepresentation(image2);
return [data1 isEqual:data2];
}

Reading the comments of your question, i think u cloud just assign the button a "state" such as "selected".
On the normal state the button cloud load the "not_favorite.png".If the user tap on the button it will be on a selected state and the corresponding image(favorite.png) will load.
On future if you need to know which image is loaded(or if the user already taped on the button to make it favorite) u can just check the button's state.
Personally i will not compare images just to fire an action for a button. It will be costly.

why not try to use imageView to identify the image?
UIImageView *imageView_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"favorite.png"]];
UIImageView *imageView_not_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"not_favorite.png"]];
//both imageView_favorite and imageView_not_favorite can be set as an member ivar, and the favoriteButton.imageView can be set to them appropriately.
if (favoriteButton.imageView == imageView_favorite) {
NSLog(#"yes!");
}

try this
must check your buttonname and extension. it must be same.
-(IBAction)btnMaleClick:(id)sender {
if (btnMale.imageView.image == [UIImage imageNamed:#"radiobtn.png"]) {
[btnMale setImage:[UIImage imageNamed:#"radiobtnselected.png"] forState:UIControlStateNormal];
[Female setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
}
else {
[btnMale setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[Female setImage:[UIImage imageNamed:#"radiobtnselected.png"] forState:UIControlStateNormal];
}
}

Related

Change image when a button is clicked using Objective-C

I am trying to switch between two different images when clicking on a button. Below is the code I wrote so far. However, this will only display one image and not toggle between them.
-(IBAction)show {
BOOL img = true;
if (img = true) {
UIImage *img1 = [UIImage imageNamed:#"UnCheck.png"];
[imageview setImage:img1];
}
else {
UIImage *img = [UIImage imageNamed:#"Check.png"];
[imageview setImage:img];
}
}
You have a couple of issues with your code:
First, img is a local variable inside your tap handler, so each time that function runs, it will be set to true.
Second, your if statement actually contains an assignment (=) instead of a comparison (==), so even if img wasn't already true it would be when you execute the if statement.
All of this means that your image is always going to be UnCheck.png.
You need to use a property, outside the function, so that the state is tracked properly. img is also a pretty poor variable name, checked or isChecked is probably better.
Then your button handler method simply needs to toggle this property and set the appropriate image.
#property BOOL isChecked;
-(IBAction)show {
self.isChecked = !self.isChecked;
NSString *imageName = self.isChecked ? #"Check.png":#"UnCheck.png";
UIImage *img = [UIImage imageNamed:imageName];
[imageview setImage:img];
}

UITableViewCell.ImageView.image Compare UIImage

I am comparing cell.imageView.image with UIImage as below code.
Please refer below Code
UIImage *imgOne = [UIImage imageNamed:#"One"];
if([customCell.imageView.image isEqual: imgOne]){
NSLog(#"Right Image");
}
While debugging, I write as po [customCell.imageView.image isEqual imgOne], but it always returns as 'nil'.
Can anyone tell me how to compare this or proper way to compare?
Generally UIImageView can not store file name of image but it store only UIImage so, it is not possible to get file name of image which you add in UIImageView.
Thats way if you want to compare only image file name then it is not possible to compare it.
But If you want to compare two UIImage object then
UIImage *secondImage = [UIImage imageNamed:#"image.png"];
NSData *imgData1 = UIImagePNGRepresentation(self.imageView.image);
NSData *imgData2 = UIImagePNGRepresentation(secondImage);
BOOL isCompare = [imgData1 isEqualToData:imgData2];
if(isCompare)
{
NSLog(#"Image View contains image.png");
}
else
{
NSLog(#"Image View doesn't contains image.png");
}
If you matain the "image one" as a member var.(1)
And set the cell's image this value.(2)
Then you can compare using those two pointers.(3)
See this code:
//1
self.imgOne = [UIImage imageNamed:#"One"];
//2
customCell.imageView.image=self.imgOne
//3
if(customCell.imageView.image == self.imgOne)
{}
Instead of Imageview I tried with Button
And than i Check as below
if([cell.button.currentImage isEqual: [UIImage imagenamed:"Your Imagenem"]]){
NSLog(#"Right Image");
}
It works fine..
Thank you.

How to compare self.button.currentImage to an image?

I am trying to create a button represented by an image which whenever is pressed changes the image to the other one so that I can know which image is currently selected.
- (IBAction)imageWasPressed:(id)sender {
UIImage *imageWork = [UIImage imageNamed:#"icn_work"];
NSData *data1 = UIImagePNGRepresentation(self.imageButton.currentImage);
NSData *data2 = UIImagePNGRepresentation(imageWork);
if (data1==data2){
[self.imageButton setImage:[UIImage imageNamed:#"icn_personal"] forState:UIControlStateNormal];}
}
I have also tried this but it didn't work:
- (IBAction)imageWasPressed:(id)sender {
if ([[self.imageButton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"icn_work"]]){
[self.imageButton setImage:[UIImage imageNamed:#"icn_personal"] forState:UIControlStateNormal];}
}
The line which changes the images works but I can't compare the two images. Any help would be much appreciated! Thank you!
This is rather unconventional approach, and I would recommend keeping track of your selection some other way.
But to answer the issue at hand, it does not work because you are comparing pointers, not data.
data1 * will always be different to data2 *.
From the documentation:
isEqualToData:
Compares the receiving data object to otherData.
- (BOOL)isEqualToData:(NSData *)otherData
If you want to change button image regarding it's state you can assign different images for different states.
[self.imageButton setImage:image1 forState:UIControlStateNormal];
[self.imageButton setImage:image2 forState:UIControlStateHighlighted];
[self.imageButton setImage:image3 forState:UIControlStateSelected];
Since you can get button state and you know what image you set for the state you can get image as well.
if ([data1 isEqualToData: data2])
instead of
if (data1==data2)

show image in different image view when button clicked.

I've been trying to make this code work but can't really get final part together.
I got 2 rows of 6 image views,
Imageview 1 to 6 and 7 to 12.
First I click 6 buttons out of my 26 buttons (A-Z) and they display in the first 6 views with this code:
- (void)setImage:(UIImage *)image {
if (imageview1.image == nil) {
[imageview1 setImage:image];
} else if (imageview2.image == nil){
[imageview2 setImage:image];
} else if (imageview3.image == nil){
[imageview3 setImage:image];
} else if (imageview4.image == nil){
[imageview4 setImage:image];
} else if (imageview5.image == nil){
[imageview5 setImage:image];
} else if (imageview6.image == nil){
[imageview6 setImage:image];
}
}
-(IBAction)showA {
UIImage *img = [UIImage imageNamed:#"A.png"];
[self setImage:img];
}
now when I click the button play I want the images to change from view 1 - 6 to 7 - 12.
I hope there is someone who can explain me how to do this, I tried a couple of ways now but Can't really get it working, maybe making variable strings then get the image from that?
hope someone can explain me or has some sample code for me thanks
Like this?
[imageview7 setImage:imageView1.image];
[imageview8 setImage:imageView2.image];
etc...
[imageview1 setImage:nil];
[imageview2 setImage:nil];
etc...
It is not because you did not set up an image for your UIImageView that it did not alloc a space for the image.
In the other other way CGImage represents the "data of your image". If you did not set up the image but UIImageView alloc the UIImage for you, you are sure that the "data of this image" will be nil (because there is no image).
So you should try that out:
if([imageview1.image CGImage]==nil){[imageview1 setImage:image];}
Hope that help you

How to check a buttons image in Xcode

I am currently trying to find out, whether it is possible to check which image a button uses.
Lets say I have image1 and image2.
If the buttons image is image1 do this and if buttons image is image2 do that.
But xcode doesnt give me any autofill options....
[m1 setImage:[UIImage imageNamed: #"Penguin.png"] forState:UIControlStateNormal];
This is how i set the images. but how do i find out wheter its Penguin or Penguin2?
Just used This Code for Button Action on the basis of image
- (IBAction)checkButtonClicked:(UIButton *)sender {
if ([checkButton.currentImage isEqual:[UIImage imageNamed:#"checkbox.png"]])
[checkButton setImage:[UIImage imageNamed:#"Checked.png"] forState:UIControlStateNormal];
//do some thing here for your image1
else
[checkButton setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
//do some thing here for your image 2
}
Cocoa Touch was designed around the Model–View–Controller pattern, so you might want to try adopting that pattern. Instead of trying to retrieve state information – the selected penguin – from the view – the button – store it in an instance variable in your controller class.
Your code for setting the image could look like this:
self.currentImage = #"Penguin.png";
[m1 setImage:[UIImage imageNamed: currentImage] forState:UIControlStateNormal];
Then when you need to check the value:
if ([#"Penguin.png" isEqual:self.currentImage]) {
do something;
}
Checking the equality of images is done like so:
if ([[UIImage imageNamed:#"Penguin.png"] isEqual:m1.currentImage]) {
// do something
}
2 methods can be used for this:
1.
UIImage *image = self.myButton.currentBackgroundImage;
2.
myImageView.image = [myButton backgroundImageForState:myButton.state];
UIImage *img=[(UIButton *) sender currentImage];
if(img == [UIImage imageNamed:#"edit"])
{
//If do something
}`
try this hope it will work
In Swift also possible
iOS 8+
if button.currentImage?.isEqual(UIImage(named: "Penguin.png")) {
//do something here
}
iOS 7-
if timerButton.currentImage == UIImage(named: "Penguin.png") {
//do something here
}

Resources