开发者

how to display Image in ImageView?

I am new to programming and developing 开发者_Python百科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"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜