iOS在UIImageView中加载图像时未显示

iOS在UIImageView中加载图像时未显示,ios,uiimageview,Ios,Uiimageview,我试图以编程方式加载背景图像,但图像未显示。这是我的密码: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { NSString *filePath = [[NSBundle mainBu

我试图以编程方式加载背景图像,但图像未显示。这是我的密码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
        UIImage *backGroundImage = [UIImage imageWithContentsOfFile:filePath];
        self.backGroundView = [[UIImageView alloc] initWithImage:backGroundImage];
        self.backGroundView.hidden = NO;

    }
    return self;
}
如果在控制台上打印UIImageView:

po _backGroundView
<UIImageView: 0x15d40cf0; frame = (0 0; 568 320); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x15d41fb0>>
po\u背景视图

任何人都知道为什么图像没有加载到屏幕上?

您正在用新的背景视图覆盖现有背景视图-只需将图像设置到现有背景视图上(加载nib时自动创建)


按照目前的代码方式,您可以使用新的视图替换nib创建的视图,该视图随后不会添加到viewController.view中。它不会显示-现有视图仍在视图层次结构中-但不会附加到属性-因此图像更改不会影响它…

您正在用新的背景视图覆盖现有的背景视图-只需在现有的背景视图上设置图像(加载nib时自动创建)


按照目前的代码方式,您可以使用新的视图替换nib创建的视图,该视图随后不会添加到viewController.view中。它不会显示-现有视图仍在视图层次结构中-但不会附加到属性-因此图像更改不会影响它…

您正在用新的背景视图覆盖现有的背景视图-只需在现有的背景视图上设置图像(加载nib时自动创建)


按照目前的代码方式,您可以使用新的视图替换nib创建的视图,该视图随后不会添加到viewController.view中。它不会显示-现有视图仍在视图层次结构中-但不会附加到属性-因此图像更改不会影响它…

您正在用新的背景视图覆盖现有的背景视图-只需在现有的背景视图上设置图像(加载nib时自动创建)


按照目前的代码方式,您可以使用新的视图替换nib创建的视图,该视图随后不会添加到viewController.view中。它不会显示-现有视图仍将存在于视图层次结构中-但不会附加到属性-因此图像更改不会影响它…

您需要从
initWithNibName:bundle:
中删除与
self.backGroundView
相关的代码,并将其添加到
viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
    UIImage *backGroundImage = [UIImage imageWithContentsOfFile:filePath];
    self.backGroundView = [[UIImageView alloc] initWithImage:backGroundImage];
    self.backGroundView.hidden = NO;
    [self.view addSubview:self.backGroundView];
}

您需要从
initWithNibName:bundle:
中删除与
self.backGroundView
相关的代码,并将其添加到
viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
    UIImage *backGroundImage = [UIImage imageWithContentsOfFile:filePath];
    self.backGroundView = [[UIImageView alloc] initWithImage:backGroundImage];
    self.backGroundView.hidden = NO;
    [self.view addSubview:self.backGroundView];
}

您需要从
initWithNibName:bundle:
中删除与
self.backGroundView
相关的代码,并将其添加到
viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
    UIImage *backGroundImage = [UIImage imageWithContentsOfFile:filePath];
    self.backGroundView = [[UIImageView alloc] initWithImage:backGroundImage];
    self.backGroundView.hidden = NO;
    [self.view addSubview:self.backGroundView];
}

您需要从
initWithNibName:bundle:
中删除与
self.backGroundView
相关的代码,并将其添加到
viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
    UIImage *backGroundImage = [UIImage imageWithContentsOfFile:filePath];
    self.backGroundView = [[UIImageView alloc] initWithImage:backGroundImage];
    self.backGroundView.hidden = NO;
    [self.view addSubview:self.backGroundView];
}

是否将
self.backGroundView
添加到视图层次结构中?是否使用故事板?文件路径有效吗?您是否尝试加载[UIImage ImageName:@“xxx”]?是否将
self.backGroundView
添加到视图层次结构中?是否使用故事板?文件路径有效吗?您是否尝试加载[UIImage ImageName:@“xxx”]?是否将
self.backGroundView
添加到视图层次结构中?是否使用故事板?文件路径有效吗?您是否尝试加载[UIImage ImageName:@“xxx”]?是否将
self.backGroundView
添加到视图层次结构中?是否使用故事板?文件路径有效吗?您是否尝试加载[UIImage ImageName:@“xxx”]?