Change image of UIImageView on button click in iPhone program - iphone

I want to change image in image viewer when I click a button.
I know this question basic but, I need an answer for this.
Please reply

First, implement the following method in a MyViewController.m (also, add method signature to MyViewController.h file):
MyViewController.m:
- (IBAction)handleButtonClick:(id)sender {
imageView.image = [UIImage imageNamed:#"blah.png"];
}
MyViewController.h:
{
IBOutlet UIImageView *imageView;
}
- (IBAction)handleButtonClick:(id)sender;
Then attach handleButtonClick action to button's Touch up inside event and imageView outlet to your image view in Interface builder.
Voila.

Related

Issue in assigning image to UIImageView iPhone

In my .h file I have:
IBOutlet UIImageView *image1;
and in .m file I am trying to assing image to this image1 like this
image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Blue-Jaanamaz-Small"]];
But the UIImageView is not showing anything. Its blank.
What can be the problem.
If you have an IBOutlet attached to your property , you do not need to allocate and initialize the imageView again.
Just try :
image1.image = [UIImage imageNamed:#"Blue-Jaanamaz-Small"];
Also use this if your imageView is not on the top of your view hierachy.
[self.view bringSubviewToFront:image1];
Make sure that your imageView is not hidden in InterfaceBuilder or somewhere in code.
And make sure your IBOutlet is correctly connected to an imageView in the InterfaceBuilder.
Check the connections between Interface builder and UIImageView.

removing a subview

I have looked on here and found a few examples but nothing works so here goes...
I have a subview which is started and works fine, the camera takes a pic and then a thumbnail appears at the bottom right, when clicked I want the subview released but no matter how I try I can't get rid of the subview!
Code:
-(void)onSingleTap:(UITouch*)touch {
NSLog(#"onSingleTap");
UIImageView *eyes = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"binocs.png"]] autorelease];
eyes.tag = EYES_TAG;
[self.overlayView addSubview:eyes];
[camera takePicture];
}
Then the thumbnail action:
- (void)thumbnailTapped:(id)sender {
self.view.alpha = 99.0f;
UIButton *binocsButton = (UIButton*)[self.view viewWithTag:BINOCS_BUTTON_TAG];
binocsButton.hidden = YES;
}
So my subView is called eyes and I just need to clear that view under the thumbnail tapped action. Any ideas would be fantastic!
I'm not sure I follow entirely, but don't you just want to add this to the bottom of thumbnailTapped::
[[self.view viewWithTag:EYES_TAG] removeFromSuperview];
By the way, rather than using tags it's probably better to store a reference to the views that you want to access as an ivar of your view controller class. I would add this to your property definitions:
#property (nonatomic, retain) UIImageView *eyes;
#property (nonatomic, retain) UIButton *binocsButton;
Then synthesise them and when you create those objects, assign to the properties and then you can access them easier later. Using tags I found in the past gets quite annoying as if the view is at the bottom of a complex view hierarchy you can't access it easily. If you do do this, remember to also release them in your dealloc (unless you're using ARC).

how to show same image in all view without implementing it in all view?

I want to show my apps logo image in top of every view. But I want to create it once in a view that show it in every view like master page in ASP.net. please some one help me.
Create a subclass of UIViewController, and in viewDidLoad method, get and place the image where your want :
.h
#interface MyLogoViewController : UIViewController
// your interface
#end
.m
#implementation MyLogoViewController
- (void) viewDidLoad {
UIImage* image = [UIImage imageNamed:#"image.png"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame.origin.x = // whatever you want
imageView.frame.origin.y = // whatever you want
[self.view addSubview:imageView];
[self.view bringSubviewToFront:imageView];
[imageView release];
}
#end
Then for any of your view controller that controls a view in wich you want that image to be displayed should inherit that subclassed View controller instead of UIViewController.
#interface MyNewViewController : MyLogoViewController {
// your interface
}
#end
That way, image displays automatically without having to do anything special.
If you plan to add subviews programmatically in your view controllers, then keep a reference for imageView into MyLogoViewController class as a member var (instead of just creating / releasing it on the fly), so you will be able to put your new views behind the existing logo if needed.
you can place that image in main.window. so you can see that image in every view which views are having subviews to main window. But you need fix all view sizes according to your images size fixed in main window.Thats enough.
You can also add the image to window and have fix the size of the view then u can see in sub view
YOu can add that view in window and add other view below that window image. so you can see that in every page.
Create a modal class for the image and call this from viewDidLoad of every controller.

How to reset the image in UIImageView without loading the whole view?

I have a xib which has 1 UIImageView and 1 Toolbar.
Toolbar has two buttons save and reset.
In the simulator i can make some drawing on the image in the UIImageView.
Now i want that if i click on the reset button then i would get the original image if i didn't save the changes on the image.
I don't want to load the entire view again.
Only I need to replace my edited image with original image?
Can you please guide me that how can i do this stuff?
IBOutlet UIImageView *imageView;
imageView.image=[UIImage imageNamed:#"img.png"];
[imageView setNeedsDisplay];
UIImageView *imageView;
imageView.image=[UIImage imageNamed:#"one.png"];
refresh with orignal image. Have u tried this
[imageView reloadInputViews];
If it not work try this code again to refresh
imageView.image=[UIImage imageNamed:#"one.png"];
Create an IBOutlet using
IBOutlet UIImageView *myImageView;
Hook this up with your xib file in interface builder and change the image with
myImageView.image=originalImage;
[myImageView setNeedsDisplay];
You can use myImageView to refer to that image view in IB.
Can you just set the image property of the UIImageView to the original image.
UIImageView *imageView = [UIImageView new];
[imageView setImage:origingalImage];

How can I make UIImageView change its image programmatically?

I have this code:
- (void)viewDidLoad {
[super viewDidLoad];
landscape.image = [UIImage imageNamed:#"tenerife1.png"];
}
First, I created an UIImageView with IBOutlet. Landscape is pointing to that UIImageView. In InterfaceBuilder I told to show an specific image in that UIImageView. It never changes. I always see the image I specified in InterfaceBuilder. But I want to set it programmatically. How can I do this?
Try calling setNeedsDisplay on the view after you change the image:
[landscape setNeedsDisplay];
Add this code, it might help you:
-(void)viewDidAppear:(BOOL)animated
{
landscape.image = [UIImage imageNamed:#"tenerife1.png"];
}
is it possible you did not have a custom view set up for that nib file? I had the same problem. I created a UIview subclass for that view controller, went into Interface Builder and set the view to that UIView subclass and then added an import header for the UIView into the Controller class. thats what did it for me
Don't forget about connection between imageview and File's Owner in InterfaceBuilder.
it's only you need after that:
landscape.image = [UIImage imageNamed:#"tenerife1.png"];