Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically add a UIImage to a UIImageView then add that UIImageView to another UIView?

As the title says, I'm trying to create a UIImage that will go inside a UIImageView (so that I can animate it later) then I want to put that UIImageView into another classes UIView.

So far my relevant code is:

This is the viewDidLoad for my root view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.playerViewController = [[TUKIPlayerViewController alloc] init];
    [self.view addSubview:playerViewController.view];
}

And this is the init for the UIImageView:

- (id)init
{
    if (self = [super init]) {
        playerIdle = [UIImage imageNamed:@"playerIdle.png"];
        playerView = [[UIImageView alloc] initWithImage:playerIdle];
        self.view = playerView;
    }
    return self;
}

It builds and runs with 0 errors.

What am I doing wrong? I don't see the playerIdle.png anywhere. Though I'm betting I'm doing this terribly wrong.

like image 912
Jarrod Avatar asked Apr 02 '12 06:04

Jarrod


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do I add an image to UIImageView?

The first step is to drag the UIImageView onto your view. Then open the UIImageView properties pane and select the image asset (assuming you have some images in your project). You can also configure how the underlying image is scaled to fit inside the UIImageView.

How do you add a UIImage in Objective C?

For example: UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]];


1 Answers

In your ViewController , add the imageview directly...

- (void)viewDidLoad
{
   playerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"playerIdle.png"]];
   [self.view addSubView: playerView];
}
like image 153
Akash Malhotra Avatar answered Nov 15 '22 18:11

Akash Malhotra