show image in different image view when button clicked. - ios

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

Related

UIImageView not showing in app

I have a UIImageView which is displayed using the following code:
profileImage = [self getImageFromUrl:#"http://i.imgur.com/l0HVdmR.png"];
if (profileImage == nil) {
NSLog(#"SourceImage is nil");
} else {
NSLog(#"SourceImage isn't nil");
}
profileImageView = [[UIImageView alloc] init];
profileImageView.image = profileImage;
[profileImageView setFrame:CGRectMake((screenWidth/2)-(profileDiameter/2), profileDiameter/2, profileDiameter, profileDiameter)];
[profileImageView setContentMode:UIViewContentModeScaleToFill];
profileImageView.userInteractionEnabled = isEditing;
NSLog(#"Online image used");
if (profileImageView == nil) {
NSLog(#"ImageView is nil");
} else {
NSLog(#"ImageView isn't nil");
}
Later on I use the code [mainScrollView addSubview:profileImageView to add the UIImageView to the UIScrollView I have set up. The UIImageView used to correctly display this image without any trouble. However, the UIImageView has suddenly stopped displaying altogether, and I'm unsure of what change elsewhere in my code caused this.
The framing isn't to blame. The variables are the correct values and changing them didn't solve the issue. The NSLogs show both the UIImage and UIImageView to not be nil. If it helps, the UITapGestureRecognizer I have set up on the UIImageView does not register taps in the area in which the image should be - This may provide some clue regarding the symptoms of the bug.
All help appreciated.
Check whether the value of profileDiameter was initialised or not. Also addSubview must be used.
Kindly upload some more details regarding how profileDiameter is found out.

Need help programmatically setting a UIImageVIew Image

I want to do is set the image of an empty UIImageView but whatever I try, nothing seems to work. Sorry if this is stupid, I am new to Xcode and Obj-C. The UIImageView takes up whole of the view, and there is a button to load the image (I have also tried putting this in the viewDidLoad bit, no success). Here is the code from my ViewController.m:
- (IBAction)buttonChangeImage:(id)sender {
UIImage *image = [UIImage imageNamed:#"1.jpg"];
[_imageView setImage:image];
}
Thanks in advance
Probably you did not connect IBOutlet _imageView with imageView in XIB or Storyboard.
Just in case you're having trouble interpreting #djromero's comment, here's how you could see if your problem is with the image:
- (IBAction)buttonChangeImage:(id)sender {
UIImage * imageToAdd = [UIImage imageNamed:#"1.jpg"];
if (imageToAdd) {
_imageView.image = imageToAdd;
NSLog(#"Image exists, if still not working, make sure _imageView is set properly");
}
else {
NSLog(#"There was a problem loading your image, make sure it is named properly");
}
if (_imageView) {
NSLog(#"ImageView exists!");
}
else {
NSLog(#"ImageView does not exist, make sure it is properly connected");
}
}

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

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

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