图像不在UIImageView中显示

时间:2013-04-11 23:20:42

标签: ios

我已经在我的视图中添加了一个UIImageVIew,如果我指定要在IB中使用的图像,那么它会正确显示,但如果我尝试以编程方式添加图像,那么它就不会显示。

我清除了要从IB加载的图像并添加了以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage* image = [UIImage imageNamed:@"pic1.png"];
    UIImageView *iv = [[UIImageView alloc] initWithImage: image];
    iv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    iv.contentMode = UIViewContentModeCenter;
    self.slideshowView = iv;
}

self.slideshowView是连接到图像视图的插座。

我可以确认图像和iv在执行时都是非空的。

1 个答案:

答案 0 :(得分:1)

不要创建新的UIImageView,而是设置插座的图像属性。这里的问题是您的新UIImageView在超级视图中没有框架,并且您要将插座重新分配给您创建的新图像视图。

self.slideshowView.image = [UIImage imageNamed:@"pic1.png"];