2013-12-07 42 views
1

我想以编程方式将UIImageView添加到我的模式视图控制器,并且图像根本不显示。我只看到白色背景。其余的工作正常,因为我能够加载或解散视图。我下面的代码:UIImageView(以编程方式创建)没有显示

[super viewDidLoad]; 
// Do any additional setup after loading the view. 
self.view.backgroundColor = [UIColor whiteColor]; 

UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)]; 
imageView.image = [UIImage imageNamed:@"IMG_0352.PNG"]; 
[self.view addSubview:imageView]; 


UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
swipeleft.direction = UISwipeGestureRecognizerDirectionLeft; 
[self.view addGestureRecognizer:swipeleft]; 

UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
swiperight.direction = UISwipeGestureRecognizerDirectionRight; 
[self.view addGestureRecognizer:swiperight]; 


UIBarButtonItem *dismiss_btn = [[UIBarButtonItem alloc] initWithTitle:@"Start App" style:UIBarButtonItemStylePlain target:self action:@selector(dismissModal:)]; 

self.navigationItem.rightBarButtonItems = [NSMutableArray arrayWithObjects:dismiss_btn, nil]; 
+1

这应该工作...尝试为imageView添加backgroundColor以检查图像中显示的问题或者imageView –

+0

全部大写的图像名称是否正确? – thorb65

+0

除了@HaniIbrahim对背景颜色的建议外,我还会在这里推荐一些'NSLog'输出,检查'imageView.image'为null。 – nhgrif

回答

0

您需要删除

UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)]; 

因为...好... init] initWithFrame:看起来像一个错字 和下面的是一个更好的方法来初始化一个 UIImageView

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMG_0352.PNG"]]; 

裁判: UIImageView only displays when I call initWithImage

+0

如果图像视图需要与图像大小不同,这并不会更好。 – rmaddy

+0

并且还需要设置框架。或者该视图应该如何知道该放置在哪里? – thorb65

+0

@visualication:我们有'setFrame'。至少我总是只是简单地init和'setFrame'稍后 – staticVoidMan

3

一个严重问题是这一行:

UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)]; 

它需要:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)]; 

您有一个额外的呼叫init

还要确保文件名真的名为IMG_0352.PNG。案件事宜。确保它不是真的IMG_0352.png或类似的东西。

当然要确定你实际上有这样一个图像被包装到你的应用程序中。确保它列在目标的“Build Phases”的“Copy Bundle Resources”部分下。

0

imageView正在初始化两次。尝试删除一个init

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)]; 

此外而如果图像/文件夹被添加作为在Xcode一个“创建组”选项,而不是与“创建文件夹参考”添加图像到图像文件夹检查。 文件夹引用只会给出链接或引用,因此您实际上无法访问文件夹中的元素。

相关问题