I am working with the tableview and Sqlite when i update my Table based on the values i need to change the image which is in tableview cell it is also working fine but when i scroll then my images are changing below is my code
Sqlite Table Update
[DBObject upDateTable:#"Green" :messageID];
How i am fetching values from my array
NSString *messageStatus = [[messageArray1 objectAtIndex:index] ColorStatus];
now I am checking colorStatus like below in cellForRowAtIndexPath
if ([messageStatus isEqualToString:#"Green"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Green.png"];
[[ASNGlobalClass shareManager] setIsDelivered:YES];
}else if ([messageStatus isEqualToString:#"Red"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Red.png"];
}else if ([messageStatus isEqualToString:#"Yellow"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-yellow.png"];
}
my images are assigning perfectly but when i scroll then it is changing i don't know where i am missing please help me out
cell.playButtonImageView.image=nil; // write this line before set image
if ([messageStatus isEqualToString:#"Green"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Green.png"];
[[ASNGlobalClass shareManager] setIsDelivered:YES];
}else if ([messageStatus isEqualToString:#"Red"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Red.png"];
}else if ([messageStatus isEqualToString:#"Yellow"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-yellow.png"];
}
if ([messageStatus isEqualToString:#"Green"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Green.png"];
[[ASNGlobalClass shareManager] setIsDelivered:YES];
}else if ([messageStatus isEqualToString:#"Red"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Red.png"];
}else if ([messageStatus isEqualToString:#"Yellow"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-yellow.png"];
}
else
cell.playButtonImageView.image= [UIImage imageNamed:#"some-default-image-placeholder.png"]; // some-default-image-placeholder.png is an placeholder image name
Related
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
}
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) {
}
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.
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];
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
}
}