Change image when a button is clicked using Objective-C - ios

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];
}

Related

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.

My UIImage is loaded but doesn't appear on screen

i'm writing an app that changes an image through different conditions. When i debug everything is set but i do not see the image on my screen. Can anybody please help me?
-(void)setLuminanceImageSource
{
self.luminanceImageView.image = nil;
UIImage *image;
switch (self.luminanceImageSource) {
case FULL_LIGHT:
image = [UIImage imageNamed:#"belichting-goed"];
[self.luminanceImageView setImage:image];
NSLog(#"full");
break;
case HALF_LIGHT:
image = [UIImage imageNamed:#"belichting-matig"];
[self.luminanceImageView setImage:image];
NSLog(#"half");
break;
case BAD_LIGHT:
image= [UIImage imageNamed:#"belichting-slecht"];
[self.luminanceImageView setImage:image];
NSLog(#"bad");
break;
default:
image = [UIImage imageNamed:#"belichting-goed"];
break;
}
[self.luminanceImageView setImage:image];
[self.luminanceImageView setNeedsDisplay];
[self.view addSubview:self.luminanceImageView];
[self.view bringSubviewToFront:self.luminanceImageView];
}
This is my method that controls the image. I have an UIImageView in the .xib file and when i run the app i see the image i have selected in attribute inspector, but it disappears when this method is called.
EDIT: I have changed the names to the conventional naming style.
This is my debug-console, the image object is not nil but the luminanceImageView stays nil.
I have created a github repository where i cloned my project in so you can see my code.
CameraApp repository
Only difference is the location of the luminaceImageView, now it is located in the middle of the screen so i can be sure that it is in view of the device.
You say that:
the image object is not nil but the luminanceImageView stays nil.
Of course, you need to create the image view first, before you can set its image. The easiest in your case is probably to create it and set its image in one go, and then add it to your view:
self.luminanceImageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:self.luminanceImageView];
EDIT (after looking at the GitHub):
You currently set the view like this:
self.luminanceImageView = [[UIImageView alloc] init];
[self.luminanceImageView setImage:image];
That means that if you set it in the xib (I have not checked that), then you override it here.
There are three problems with this:
the luminanceImageView still needs to be added to your view, as I
already stated above
the plain init creates an imageView with
size (0, 0). This is unlike the initWithImage:, which creates a imageView with the size of the image.
the setImage: does not resize the imageView. From the
UIWebView documentation:
Setting the image property does not change the size of a UIImageView. Call sizeToFit to adjust the size of the view to match the image.
If you have correctly set the imageView with its size in the XIB, then leave out everything else and just do:
[self.luminanceImageView setImage:image];
If the size is not correct, then follow this with:
[self.luminanceImageView sizeToFit];
EDIT (after debugging the code):
Your problem is that you set the image from a background thread. Almost all view operation in iOS need to be done from the main thread. Doing like this solves your problem:
dispatch_async(dispatch_get_main_queue(), ^{
[self.luminanceImageView setImage:image];
[self.view bringSubviewToFront:self.luminanceImageView];
});
BTW, you also change self.layer in your code. That is probably not a good idea and may lead to problems later. Use a separate view for the camera, and use [cameraView.layer addSublayer:newLayer] to get around that.
try this edit from your code
-(void)setLuminanceImageSource
{
[self.sess stopRunning];
//[self.layer removeFromSuperlayer];
UIImage *image;
switch (self.luminanceImageSource) {
case FULL_LIGHT:
{
image = [UIImage imageNamed:#"belichting-goed.png"];
NSLog(#"full");
break;
}
case HALF_LIGHT:
{
image = [UIImage imageNamed:#"belichting-matig.png"];
NSLog(#"half");
break;
}
case BAD_LIGHT:
{
image= [UIImage imageNamed:#"belichting-slecht.png"];
NSLog(#"bad");
break;
}
default:
{
image = [UIImage imageNamed:#"belichting-goed.png"];
break;
}
}
[self.luminanceImageView setImage:image];
[self.layer addSublayer: self.luminanceImageView.layer];
//[self.view bringSubviewToFront:self.luminanceImageView];
// NSLog(#"%#", self.luminanceImageView.image);
// NSLog(#"lumi X:%f, Y:%f", self.luminanceImageView.frame.origin.x, self.luminanceImageView.frame.origin.y);
// NSLog(#"view X:%f, Y:%f",self.view.frame.origin.x, self.view.frame.origin.y);
// NSLog(#"window %#", self.view.window);
}

Change Back And Forth UIImages

Hi I’m trying to get an image to change on a method call and if the method is recalled the original image comes back
-(void)change
{
if ((player.image = [UIImage imageNamed:#"first.png"]))
{
player.image = [UIImage imageNamed:#"second.png"]));
}
else
{
player.image = [UIImage imageNamed:#"first.png"]));
}
}
This works change the "first.png" image to "second.png" but when called again it doesn’t.
Where am I going wrong?
As pointed out by Michael Dautermann, the way you're comparing two UIImages is wrong. Can't you simply keep an NSString property that tells you the image you're showing?
-(void)change
{
if ([self.displayedImageTitle isEqualToString:#"first.png"]){
player.image = [UIImage imageNamed:#"second.png"]));
self.displayedImageTitle = #"second.png";
}
else{
player.image = [UIImage imageNamed:#"first.png"]));
self.displayedImageTitle = #"first.png";
}
}
In your code above, you're making an invalid assumption that "[UIImage imageNamed: #"first.png"]" will always be the same UIImage object when you do a comparison. That isn't true. Every time you call the "imageNamed" API, you may be creating a brand new UIImage object.
Now, since UIImage object doesn't know anything about file names or image titles, you need to keep track of which image is being displayed and then toggle on that.
I'd suggest creating a subclassed UIImage object that has a filename or title property (that you would also need to set), or have your two UIImages as properties or ivars loaded into memory (that is, if these aren't giant memory hogging images) and then do your toggling.

Image comparison with button image

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];
}
}

Adding UIAnimation

I have a ScrollView with paging and I have a button which takes me to an array with image in my scrollView .. Here's my codes
-(IBAction)scrollToPage:(id)sender {
int scrollToPage = 1;
int scrollToX = 1-1;
[self.scrollView scrollRectToVisible:CGRectMake(scrollToX*self.scrollView.frame.size.width,0,self.scrollView.frame.size.width,self.scrollView.frame.size.height) animated:YES];
}
I know it's already animated but I need to add custom animation to it for example :"flip horizontal, cross dissolve, etc"
Thanks in advane
You can use the following code snippet to flip the image you have in your button pressed action method. This will allow the image to be flipped then you can play around with it to be what you want.
UIImage* sourceImage = [UIImage imageNamed:#"whatever.png"];
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale:1.0 orientation: UIImageOrientationUpMirrored];
See if this works for you.

Resources