简体   繁体   中英

How to add image in image view ios

I have a view controller,on top of this I have added a image view.Now I wanted to load some image dynamically to that image view.Fallowing is my code:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (getAnySelectedRow == 0)
{
    //Load corresponding image
    NSLog(@"1 image loaded");
}
else if (getAnySelectedRow == 1)
{
    //Load corresponding image
    NSLog(@"2 image loaded");
}
else if (getAnySelectedRow == 2)
{
    //Load corresponding image
    NSLog(@"3 image loaded");
}

}

Note: Image view is already loaded on top of view controller.So I believe no need to init new image view.

How can I do this?

This is actually quite simple. UIImageView , has an image property of type UIImage . All you have to do is create a UIImage object and then assign it to this property. If the image exists within your applications bundle, you can use +[UIImage imageNamed:]`.

myImageView.image = [UIImage imageNamed:@"myImageName"];

Or to expand a little further..

if (getAnySelectedRow == 0) {
    myImageView.image = [UIImage imageNamed:@"myImageName"];
}else if (getAnySelectedRow == 1) {
    myImageView.image = [UIImage imageNamed:@"myOtherImageName"];
}else{
    myImageView.image = [UIImage imageNamed:@"myAnotherDifferentImageName"];
}

x, y are the position the image view located, width and height are the size of the ImageView

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];

imageView.image = [UIImage imageNamed:@"your image"]; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM