How to display image in ImageView?

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

+4
source share
5 answers

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 a png image that you import into your resources folder.

Using Nib (Xib file):

  • Open your.xib file.
  • Go to Tools → Library.
  • Drag one UIImageView onto your .xib.
  • Open the Attribute Inspector ImageView (press Cmd + 1).
  • You will get one Image property. Select the desired image from the Resources folder in the drop-down list.
  • There you have it. :-)
+6
source
 // First way myImage.image = [UIImage imageNamed:@"myImage.png"]; // Second way NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/myImage.png"]; myImage.image = [UIImage imageWithContentsOfFile:fullpath]; 

Check the UIImageView class description here

+1
source

Try with:

 imgView.image = [UIImage imageNamed:@"test.png"]; 
0
source

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]; 
0
source
 UIImageView *imageView=[[UIImageView alloc] init]; [imageView setImage:[UIImage imageNamed:@"yourImage.png"]]; 

(If you created an imageView from IB and left it, then ignore the first line)

0
source

All Articles