简体   繁体   English

如何以编程方式将UIImage添加到UIImageView然后将该UIImageView添加到另一个UIView?

[英]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 . 正如标题所说,我正在尝试创建一个UIImage ,它将进入UIImageView (以便我可以在以后设置动画)然后我想将UIImageView放入另一个类UIView

So far my relevant code is: 到目前为止我的相关代码是:

This is the viewDidLoad for my root view controller: 这是我的根视图控制器的viewDidLoad

- (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 : 这是UIImageView的init:

- (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. 它构建并运行0错误。

What am I doing wrong? 我究竟做错了什么? I don't see the playerIdle.png anywhere. 我在任何地方都看不到playerIdle.png。 Though I'm betting I'm doing this terribly wrong. 虽然我打赌我这样做非常错误。

In your ViewController , add the imageview directly... 在ViewController中,直接添加imageview ...

- (void)viewDidLoad
{
   playerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"playerIdle.png"]];
   [self.view addSubView: playerView];
}

Try this: 尝试这个:

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"name.png"]];
[self.view addSubview:imgView];

Pls try the below code : 请尝试以下代码:

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

Hope this might help you..... 希望这可能对你有所帮助.....

You should use [self.view addSubview:playerView]; 你应该使用[self.view addSubview:playerView]; instead of just setting it( self.view = playerview; ). 而不只是设置它( self.view = playerview; )。 What I also do most of the times is bringing that subview to the front: 我大部分时间也做的是将该子视图带到前面:

[self.view bringSubviewToFront:playerView];

Hope it helps 希望能帮助到你

或者您可以尝试: myUIImageViewObject.image = myUIImageObject;

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"name.png"]]; UIImageView * imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@“name.png”]]; imagView.frame=CGRectMake(40,130,45,45); imagView.frame = CGRectMake(40,130,45,45);

[self.view addSubview:imgView]; [self.view addSubview:imgView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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