how to display Image in ImageView? - iphone

I am new to programming and developing my first application, but I want to know that how can I display image to imageView.

Programmatically :
From http://www.iphoneexamples.com/:
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
Where myImage.png is an png format image which you will import to your resources folder.
Using Nib(Xib File):
Open your.xib file.
Go to Tools -> Library.
Drag and drop one UIImageView on your .xib.
Open Attribute Inspector of the ImageView(Press Cmd + 1).
You'll get one Image property. Set the image you want from your Resource folder using the drop-down list.
There you got it. :-)

// First way
myImage.image = [UIImage imageNamed:#"myImage.png"];
// Second way
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/myImage.png"];
myImage.image = [UIImage imageWithContentsOfFile:fullpath];
Check out UIImageView class reference here

Read http://www.iphoneexamples.com/
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

UIImageView *imageView=[[UIImageView alloc] init];
[imageView setImage:[UIImage imageNamed:#"yourImage.png"]];
(If you have created imageView from IB and have outlet-ed it then ignore First Line)

Try it using:
imgView.image = [UIImage imageNamed:#"test.png"];

Related

Setting retina background images appear zoomed in

I'm setting a random background image for my app by loading an image and adding it to my view:
UIImageView *background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:#"bg%u.jpg", 1+arc4random_uniform(15)]]];
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
My images are 640x1136 at 326 ppi, yet the image is appearing zoomed in for some reason. Any ideas as to why would be appreciated.
Simulator
Actual image:
Thanks.
It s because you alloc init with your image, not with a fix size.
int screenHeight = [UIScreen mainScreen].bounds.size.height;
int screenWidth = [UIScreen mainScreen].bounds.size.width;
UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
background.image = [UIImage imageNamed:[NSString stringWithFormat:#"bg%u.jpg",1+arc4random_uniform(15)]]
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
Do it like this
Hi try like this,
UIImageView *background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:#"bg%u.jpg", 1+arc4random_uniform(15)]]];
background.frame = self.view.frame; // this is the only line you have missed
[self.view addSubview:background];
[self.view sendSubviewToBack:background];

animated gif on uibutton

I use this code, that work, really size of image 160x148 but image button is view very big on all screen!
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"button1.png"],
[UIImage imageNamed:#"button2.png"],
[UIImage imageNamed:#"button3.png"],
[UIImage imageNamed:#"button4.png"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[yourButton addSubview: animatedImageView];
How fix that?
The images are big because you called initWithFrame: and passed in your view's bounds. Change it to a different-sized rect, such as:
CGRectMake(0, 0, 160, 148)
... and then reposition it as you see fit.
Try this code.It will help you for correct solution.
UIImage *myimage=UIImage imageNamed:#"button1.png";
Button.imageview.image=myimage;

error while setting image in imageView in iphone

I have written a code for setting imageview ...as per the button clicked...but my problem is when i press the button it is giving me the error like...too many arguments to function ImageNamed...while executing this line...
UIImage* photo = [UIImage imageNamed:#"robort %d.jpg",x];
the whole code is....
-(void)buttonClicked:(UIButton*)button
{
NSLog(#"%d",button.tag);
int x;
x=button.tag;
// Create the image view.
UIImage* photo = [UIImage imageNamed:#"robort %d.jpg",x];
NSLog(#"%d",x);
CGSize photoSize = [photo size];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[self.view addSubview:imageView]; }
use UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort de nero%d.jpg",x]];
cheers :)
Try this
-(void)buttonClicked:(UIButton*)button
{
NSLog(#"%d",button.tag);
int x;
x=button.tag;
// Create the image view.
NSString *imageName = [NSString stringWithFormat:#"robort %d.jpg",x];
UIImage* photo = [UIImage imageNamed:imageName];
NSLog(#"%d",x);
CGSize photoSize = [photo size];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[self.view addSubview:imageView];
}
You need to use:
UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort %d.jpg",x]];
Use this
UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort %d.jpg",x]];

how to show image on right hand side of table view for iphone

i can show image on left hand side of table view but why my right hand side is not working
CGRect frame;
frame= CGRectMake(310 ,10, 50, 50);
UIImageView * myImageView = [[UIImageView alloc]init];
cell.myImageView.image = [UIImage imageNamed:#"bowl.png"];
myImageView.frame = frame;
[myImageView release];
Try this, it works for me:
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yellow_dot.png"]];
cell.accessoryView = image;
Do this code in willDisplayCell and it should work. Also remember the default cell already has an imageview, you can just move its frame.

Why won't my UIImageView appear as a subview?

UIImage *image = [[UIImage alloc] initWithContentsOfFile:#"popup.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[image release];
[imageView release];
The code sits within a UIViewcontroller object. It compiles fine but, when run, the subview does not appear to be added.
I'm tired.
initWithContentsOfFile requires the complete file path, not just the file name.
Replace the line where you initialize UIImage with the following:
NSBundle *bundle = [NSBundle mainBundle];
UIImage *image = [[UIImage alloc] initWithContentsOfFile: [bundle pathForResource:#"popup" ofType:#"png"]];
A simpler way will be to use the imageNamed: method of the UIImage class
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"popup.png"]];
And you dont have to worry about the path-to-file details. Just that the initWithImage: method caches the image. Depending upon your application might be good or bad idea.
Hope this helps!
Try to use:
UIImage *image = [UIImage imageNamed:#"popup.png"];
and, off course, get rid of the [image release]; because in this solution the image is autoreleased.
Notice that this solution won't be good if you have more than one image with this name (in different folders/groups)...
your imageView needs a frame.
imageView.frame = CGRectMake(0,0,320,480);
I know this question is old now, but some users can still benefit from an answer.
Like others have said, try using [UIImage imagedNamed:] instead of initWithContentsOfFile.
And the reason it is not showing up on screen is you did not set a location for the image. You can fix this by either setting a frame for the image view or setting its center at a single point. To place the image view in the center of the screen:
UIImage *image = [UIImage imageNamed:#"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.center = [self.view center];
// or if you want to set a frame instead
// CGRect imageFrame = CGRectMake(x,y,width,height);
// [imageView setFrame:imageFrame];
[self.view addSubView:imageView];
Enjoy :)