how to compare image with button image - ios

I am trying to comparing image with button images but my code is not working for this requirement. What did I do here wrong? Please help me.
NSString *btnImage;
UIButton * sharebutton;
btnImage = #"arrow.png";
[sharebutton setImage:[UIImage imageNamed: btnImage] forState:UIControlStateNormal];
if ([sharebutton.imageView.image isEqual:[UIImage imageNamed:#"arrow.png"]])
{
NSLog(#"ok");
}
else
{
NSLog(#"else");
}

you can check image for specific state
if([[sharebutton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"arrow.png"]])
{
//Image found
}
Maybe this will help you.

if (sharebutton.currentImage == UIImage(named: "arrow.png")) {
}
U will need to convert this into obj-c but i think u will get it. :)

You could use the following code:
if ([UIImagePNGRepresentation(firstImage) isEqualToData:UIImagePNGRepresentation(secondImage)])

If you want to compare button image with another image name then set your button image this way:
sharebutton.setImage(UIImage(named: "arrow.png"), forState: UIControlState.Normal)
Remember one thing here don't set background image for button.
Now you can compare button image with another image this way:
if (sharebutton.currentImage?.isEqual(UIImage(named: "arrow.png")) != nil) {
println("YES")
} else {
println("NO")
}

UIImage *image = [sharebutton imageForState:UIControlStateNormal];
if ([image isEqual:[UIImage imageNamed:#"arrow.png"]]) {
}
Do this,you can solve it.

This is what i'm using:
NSData *leftImageData = UIImagePNGRepresentation([sharebutton imageForState:UIControlStateNormal]);
//take note that using `imageForState:` only works if you set your image using `setImage:forState:`
//if you are using `setBackgroundImage:forState:`, `[shareButton currentBackgroundImage]` is what you need.
NSData *rightImageData = UIImagePNGRepresentation([UIImage imageNamed:#"arrow.png"]);
if ([leftImageData isEqualToData:rightImageData])
{
//same
}
else
{
//not same
}

For performance reasons I'd compare the file names rather than creating a temporary image which is much more expensive
NSString *btnImage;
UIButton *sharebutton;
btnImage = #"arrow.png";
[sharebutton setImage:[UIImage imageNamed: btnImage] forState:UIControlStateNormal];
if ([btnImage isEqualToString:#"arrow.png"])
{
NSLog(#"ok");
}
else
{
NSLog(#"else");
}
Of course this requires to maintain the variable btnImage to hold always the current file name

Try this:
UIButton *sharebutton;
UIImage *image1 = [UIImage imageNamed:#“arrow.png"];
[sharebutton setImage:image1 forState:UIControlStateNormal];
if (sharebutton.currentImage == [UIImage imageNamed:#"arrow.png"])
{
//do something here
}

Related

iOS: How to check what the button background image is?

I am trying the following method ,but it isn't work. Is there a way I can check what my current background image is for my UIButton and compare it?
UIImage *buttonCheckImage = checkButton.currentBackgroundImage;
UIImage *checkImage = [UIImage imageNamed:#"checkBox.png"];
if (buttonCheckImage == checkImage) {
}
this works using
[ UIImagePNGRepresentation( uiImage1 ) isEqualToData:
UIImagePNGRepresentation( uiImage2 ) ];
UIImage *buttonCheckImage = [self.btn_Checkbox imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:#"checkbox"];
if([UIImagePNGRepresentation(buttonCheckImage) isEqualToData:UIImagePNGRepresentation(checkImage)])
{
self.btnImage = [UIImage imageNamed:#"checkbox1"];
}
else
{
self.btnImage = [UIImage imageNamed:#"checkbox"];
}
[self.btn_Checkbox setImage:self.btnImage forState:UIControlStateNormal];
using : http://stackoverflow.com/questions/14078283/comparing-two-images-whether-same-or-not-ios
Hope its working well try this....
.h
#property (weak, nonatomic) IBOutlet UIButton *checkButtonObj;
-(IBAction)buttonClick:(id)sender;
.m
-(void)viewDidLoad {
[super viewDidLoad];
[self.checkButtonObj setImage:[UIImage imageNamed:#"correct.png"] forState:UIControlStateNormal];
}
-(IBAction)buttonClick:(id)sender {
if ([self.checkButtonObj.imageView.image isEqual:[UIImage imageNamed:#"correct.png"]])
[self.checkButtonObj setImage:[UIImage imageNamed:#"wrong.png"] forState:UIControlStateNormal];
else
[self.checkButtonObj setImage:[UIImage imageNamed:#"correct.png"] forState:UIControlStateNormal];
}
Try the following code,
UIImage *buttonCheckImage = [checkButton imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:#"checkBox.png"];
if (buttonCheckImage == checkImage) {
}

Change the background color of selected TextField

I am creating an app in which I have used user registration.In registration page, I have created many UITextFields.I wants that when a user select any TextField then its background Image is change. I uses the code to do that is:
- (IBAction)touchBegin:(id)sender
{
//UILabel *opt = (UILabel *)[cell viewWithTag:101];
if (_text1.isSelected) {
_text1.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text2.isSelected){
_text2.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text3.isSelected){
_text3.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text4.isSelected){
_text4.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else{
}
}
But I am not getting the result according to my need.
What should I use to get correct output?
be sure whether your textfield border style is UITextBorderStyleNone.
yourTextField.borderStyle = UITextBorderStyleNone;
yourTextField.background = [UIImage imageNamed:#"image.png"];
I think that it's better and easier to use UITextFieldDelegate methods like this, for example:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"big_star_non_rating"];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"AnImageForNormalStateOrSetNilBackground"];
return YES;
}
Hope it helps.

UIImage View reset/empty

HI all I am developing an iOS app which allows the user to pick an image using UIImagePicker. Now I have to create the action that restores the UIIageView with no image inside. can anyone help me with code?
-(IBAction)emptyImage{
//code to empty the UIImageView
}
-(IBAction)emptyImage{
imageView.image=[UIImage imageNamed:#"noimage.png"]; (OR)
imgview.image= nil; (OR)
imgview.image= [UIImage imageNamed:#""];
}
Try to use this one....
-(IBAction)emptyImage
{
imageView.image=nil
}
-(IBAction)emptyImage
{
NSArray *remoVeSunBVIew = [self.view subviews];
for(UIView *viw in remoVeSunBVIew)
{
[viw removeFromSuperview];
}
}
Hope this will help you..

How to get a button's current image using imageForState?

I found a property called imageForState in xcode but am having trouble getting it to do what I want. When a button is pressed, I want to execute a block of code depending on the button's image.
- (IBAction)favButton:(UIButton *)sender {
NSString *currentImage = [sender imageForState:UIControlStateNormal];
if([currentImage isEqualToString:#"already_fav"])
{
// execute code
}
}
However, I am getting the error:
Incompatible pointer types initializing NSString _strong with an expression type of UIImage
Can someone please tell how to get around this?
I am sure that you are not able to compare a string with an image. There is a solution however. All you have to do is set the tag for the image you are wanting to compare, and set the tag of the image you are comparing to.
image1.tag = 1;
image.tag =1;
if(image1.tag == image.tag) {
// execute code
}
that should help, and I hope that it does.
SO, for this exercise, i'll show you. Change the NSString to a UIImage
UIImage *currentImage = [sender imageForState:UIControlStateNormal];
currentImage.tag = 1;
wantedImage.tag = 1;
if(currentImage.tag == wantedImage.tag) {
// do something
}
hopefully this helps you out :)
When you create a UIImage it is just an image and has no string value attached. The name is not carried by the UIImage object.
If it were me (and this isn't the only solution, just another suggestion), i'd create individual UIImage objects (depending on how many we're talking about here) as this should checkable while strings are not.
For example:
UIImage *image1 = [UIImage imageNamed:#"randomName"];
UIImage *image2 = [UIImage imageNamed:#"anotherRandomName"];
[myButton setImage:image2 forState:UIControlStateNormal];
if ([myButton imageForState:UIControlStateNormal] == image1) {
NSLog(#"The button shows image 1 for normal state");
}
else if ([myButton imageForState:UIControlStateNormal] == image2) {
NSLog(#"The button shows image 2 for normal state");
}
else {
NSLog(#"Error!!!!!!! :D");
}
you cant save the Image as a String
try
UIImage *currentImage = [sender imageForState:UIControlStateNormal];
than you can check:
if(currentImage == [UIImage imageNamed:#"already_fav"])
I did this for switching sound/noSound image on button:
- (IBAction)soundAction:(id)sender {
UIImage *sound = [UIImage imageNamed:#"sound"];
UIImage *noSound = [UIImage imageNamed:#"noSound"];
if ([[sender currentImage] isEqual:sound]) {
NSLog(#"IF SOUND IMAGE");
[sender setImage:noSound forState:UIControlStateNormal];
//your code
} else if([[sender currentImage] isEqual:noSound]) {
NSLog(#"IF NO SOUND IMAGE");
[sender setImage:sound forState:UIControlStateNormal];
//your code
}
}

Toggle the button Image

After i implemented a code for give an check image for a button but i want to give check image for no of buttons.
note: if i click on one button check image can be displayed and the same time i click on another button check image displayed on that particular button and previous button comes normal position.
i implement the code for single button here like this.
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"btn_check_on.png"];
[self setImage:img forState:UIControlStateNormal];
}
else
{
UIImage* img = [UIImage imageNamed:#"bread_Wheat_rectangle.png"];
[self setImage:img forState:UIControlStateNormal];
}
}
The above code is executed successfully but how to use this code for no of buttons.
please suggest any tutorial regarding my problem
This is how I have implemented it for one button. You can use it for more buttons too.
-(IBAction)ButtonAction
{
if (Flag==0)
{
Flag=1;
[myButton setImage:[UIImage imageNamed:#"checkbox-filled.png"] forState:UIControlStateNormal];
}
else
{
[myButton setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
Flag=0;
}
}
Note : If you want only one button to get checked Just set all other buttons image as checkbox.png and the selected one's checkbox-filled.png.
EDIT
You can make a class for checkbox and then use it. Here is the code...
CheckButton.h
#import <Foundation/Foundation.h>
#interface CheckButton : UIButton {
BOOL _checked;
int chkButtonClickVal;
}
#property (nonatomic, setter=setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
-(int)chkButtonClickVal;
#end
CheckButton.m
#import "CheckButton.h"
#implementation CheckButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
chkButtonClickVal=0;
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) dealloc
{
chkButtonClickVal=0;
[super dealloc];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"checkbox-checked.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=1;
}
else
{
UIImage* img = [UIImage imageNamed:#"checkbox.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=2;
}
//NSLog(#"%d",chkButtonClickVal);
}
-(int)chkButtonClickVal
{
return chkButtonClickVal;
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
#end
I have done it in same way. Try you'll be able to achieve it.
Good Luck :)
After long practicing this problem we can use switch cases it can be done very easily
switch (currenttagvalue) {
case 1:
[level1 setImage:[UIImage imageNamed:#"one_time_selected.png"] forState:UIControlStateNormal];
[level2 setImage:[UIImage imageNamed:#"ic_launcher.png"] forState:UIControlStateNormal];
[level3 setImage:[UIImage imageNamed:#"bread_sourdough_rectangle.png"] forState:UIControlStateNormal];
}
in this level is "IBOutlet UIButton level1;"
And then i implement so more buttons

Resources