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

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) {
}

Related

how to compare image with button image

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
}

Using a NSArray to display an image using UIImage

I am trying to use an ImageView to be able to display any number between 1 and 6 and be able to display this on a iPhone storyboard but I am having a little difficulty with this. I have using the arc4random() % 6 method to be able to randomize the numbers between 1 and 6, the main problem is being able to display the images that I have uploaded and be able to display this on the simulator. Here is the code I have so far.
This is in the ViewConroller.m
- (IBAction)Button:(UIButton *)sender {
// In the viewDidLoad method:
[_Dice1 sizeToFit];
UIImage *image1 = [UIImage imageNamed:#"dice-1-md.png"];
UIImage *image2 = [UIImage imageNamed:#"dice-2-md.png"];
UIImage *image3 = [UIImage imageNamed:#"dice-3-hi.png"];
UIImage *image4 = [UIImage imageNamed:#"dice-4.png"];
UIImage *image5 = [UIImage imageNamed:#"dice-5-md.png"];
UIImage *image6 = [UIImage imageNamed:#"dice-6-hi.png"];
NSArray*_imageView = [[NSArray alloc]initWithObjects:#[image1, image2, image3, image4, image5, image6], nil];
int dice1 = arc4random() % 6;
if(dice1 == 0){
[_imageView objectAtIndex:0];
}
if(dice1 == 1){
[_imageView objectAtIndex:1];
}
if(dice1 == 2){
[_imageView objectAtIndex:2];
}
if(dice1 == 3){
[_imageView objectAtIndex:3];
}
if(dice1 == 4){
[_imageView objectAtIndex:4];
}
if(dice1 == 5){
[_imageView objectAtIndex:5];
}
}
#end
This is in the ViewController.h
- (IBAction)Button:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UIImageView *Dice1;
Do you guys have any suggestions? Any help would greatly be appreciated. Thanks!
So whats problem? Assign your images to your UIImageView (self.Dice1).
self.Dice1.image=[_imageView objectAtIndex:dice1];

UIimage will not move on btn press, what am i doing wrong?

I am trying to move a UIImage, first button press creates the image and second press moves it.
The image only needs to exist upon pressing the button.
In the simulator it creates the button and places it, the second time it click just doesn't do anything.
This is my Code
- (IBAction) btn:(id)sender {
UIImageView *myImage = [[UIImageView alloc] init];
myImage.image = [UIImage imageNamed:#"keyframe"];
if (startUp == 1){
//Create Image and add to view
myImage.frame = CGRectMake(200, 300, 10, 10);
myImage.image = [UIImage imageNamed:#"keyframe"];
[self.view addSubview:myImage];
//Set startUp to 0 and output rect value
startUp = 0;
NSLog(#"currentFrame %#", NSStringFromCGRect(myImage.frame));
}else if (startUp == 0){
//Change position, size and log to debug
myImage.frame = CGRectMake(500,100 ,20, 20);
NSLog(#"newFrame %#", NSStringFromCGRect(myImage.frame));
}
}
How do you programmatically move a programmatically added UIimage?
I tried changing the center value but that doesn't work either.
Try something like this – tested and working sample:
#import "ViewController.h"
#interface ViewController () {
BOOL startUp;
UIImageView *myImage;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
startUp = YES;
}
- (IBAction)doWork:(id)sender {
if (startUp) {
UIImage *img = [UIImage imageNamed: #"keyframe"];
myImage = [[UIImageView alloc] initWithImage: img];
[myImage sizeToFit];
[myImage setCenter: CGPointMake(200, 300)];
[self.view addSubview: myImage];
startUp = NO;
} else {
[myImage setCenter: CGPointMake(400, 500)];
}
}
#end

change UIButton image with array

novice need help! idea: when you press the button you need to change the image of the array. Yes, I'm stupid.
from *.h:
{
NSMutableArray *_cat;
NSInteger _catindex;
}
#property (weak, nonatomic) IBOutlet UIButton *catbtn;
- (IBAction)catbutton:(id)sender;
from *.m:`
- (void)viewDidLoad
{
[super viewDidLoad];
_cat = [NSMutableArray array];
UIImage *cat1 = [UIImage imageNamed:#"cat1.png"];
UIImage *cat2 = [UIImage imageNamed:#"cat2.png"];
UIImage *cat3 = [UIImage imageNamed:#"cat3.png"];
UIImage *cat4 = [UIImage imageNamed:#"cat4.png"];
UIImage *cat5 = [UIImage imageNamed:#"cat5.png"];
[_cat addObject:cat1];
[_cat addObject:cat2];
[_cat addObject:cat3];
[_cat addObject:cat4];
[_cat addObject:cat5];
self.catbtn.image = [_cat objectAtIndex:_catindex];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)catbutton:(id)sender
{
_catindex++;
if (_catindex == _cat.count)
{
_catindex = 0;
}
self.catbtn.image = [_cat objectAtIndex:_catindex];
}
I do not know whether it is possible to do that, but I hope for your help.
self.catbtn.image = [_cat objectAtIndex:_catindex];
won't work, you need to use:
[self.catbtn setImage: [_cat objectAtIndex:_catindex] forState:UIControlStateNormal];
forState can be used to set the image for when the button is selected / highlight etc.
use this to set image for UIButton
[self.catbtn setImage:[_cat objectAtIndex:_catindex] forState:UIControlStateNormal];
Instead of this:
self.catbtn.image = [_cat objectAtIndex:_catindex];
write this:
[self.catbtn setImage:[_cat objectAtIndex:_catindex] forState:UIControlStateNormal];
in both places.

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