2013-12-07 38 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一個「創建組」選項,而不是與「創建文件夾參考」添加圖像到圖像文件夾檢查。 文件夾引用只會給出鏈接或引用,因此您實際上無法訪問文件夾中的元素。

相關問題