2014-02-06 99 views
1

我有一個圖像(.png文件),我想放置在ViewController中的ImageView中。我使用下面的代碼,但模擬器給了我一個沒有圖像的空白視圖。 .png文件與ViewController文件位於同一目錄中。下面是代碼:以編程方式將圖像放在UIViewController上的UIImageView中

@implementation ViewController 
{ 
    NSArray *_pArray; 
    UIImage *_image; 
    UIImageView *_imageView; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _image = [[UIImage alloc] initWithContentsOfFile:@"TM-1P2.png"]; 
    _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds]; 
    [_imageView setImage:_image]; 
    [_imageView setContentMode:UIViewContentModeScaleAspectFit]; 
    [self.view addSubview:_imageView];  
} 
+0

這是視圖控制器的根視圖控制器你應用程序嗎?你有沒有嘗試改變視圖控制器的背景顏色,以確保它出現? – bneely

回答

4

如果您檢查在_image(無論是NSLog或在調試器),它可能是nil。隨着initWithContentsOfFile你應該指定整個路徑,例如:

NSString *path = [[NSBundle mainBundle] pathForResource:@"TM-1P2" ofType:@"png"]; 
_image = [[UIImage alloc] initWithContentsOfFile:path]; 

或者,您可以使用以下,這在束自動查找圖像:

_image = [UIImage imageNamed:@"TM-1P2.png"]; 

後者的語法,imageNamed,緩存圖像(即使您關閉視圖控制器也會將其保存在內存中)。如果你不得不在整個應用程序中一次又一次地使用相同的圖像(因爲它不必每次都重新加載),但是如果你只使用一次,那麼這很好,但你可能不想使用imageNamed。由於imageNamed文件說:

如果將只顯示一次的圖像文件,並希望確保它不會添加到系統的緩存,你應該使用imageWithContentsOfFile:而是創建你的形象。這將使您的一次性圖像保留在系統映像緩存之外,這可能會改善應用程序的內存使用特性。

請注意,這兩個都假定您已成功將此圖像添加到您的包中。

如果,另一方面,該圖像是在你的Documents文件夾,你可以加載它像這樣:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
NSString *path = [documentsPath stringByAppendingPathComponent:@"TM-1P2.png"]; 
_image = [[UIImage alloc] initWithContentsOfFile:path]; 

最後,需要注意的是,iOS設備是大小寫敏感的(一般模擬器是不是) ,所以請確保你的大小寫正確。


無關你的問題,在可能不應該在@implementation定義括號之間的變量,而是你應該把它們放在一個@interface。例如,你可以把它們放在.h文件中,或者更好的,你可以把它們放在一個專用類延伸在.m文件中,@implementation前右:

@interface ViewController() 
{ 
    NSArray *_pArray; 
    UIImage *_image; 
    UIImageView *_imageView; 
} 
@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"TM-1P2" ofType:@"png"]; 
    _image = [[UIImage alloc] initWithContentsOfFile:path]; 
    _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds]; 
    [_imageView setImage:_image]; 
    [_imageView setContentMode:UIViewContentModeScaleAspectFit]; 
    [self.view addSubview:_imageView];  
} 

// ... 

@end 
+0

感謝您的所有信息! imageName:工作。 –

0

有你的調試器帶臺階的貫通?

我希望看看_image是否爲非零 - 最可能的原因是沒有將它添加到您的項目中。

0

如果你有你的形象在你的包命名爲「TM-1P2.png」,你可以簡單地做到以下幾點:

_image = [UIImage imageNamed:@"TM-1P2.png"];

+0

作爲一個慷慨的人,除了接受最合適的和/或快速的答案之外,你可以對所有正確的答案投票。 – Rajiv

0
- (void)viewDidLoad { 

[super viewDidLoad]; 

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)]; 

imgView.image = [UIImage imageNamed:@"emptyCart.jpeg"]; 

imgView.backgroundColor = [UIColor whiteColor]; 

imgView.contentMode = UIViewContentModeCenter; 

[self.view addSubview: imgView]; 

} 
+0

你能解釋爲什麼這解決了OP的問題嗎? – mjuarez

+0

,因爲你需要替換這個: _image = [[UIImage alloc] initWithContentsOfFile:@「TM-1P2.png」]; 作者: _image = [UIImage imageNamed:@「TM-1P2.png」]; –

+0

所以你應該替換這一行: _image = [[UIImage alloc] initWithContentsOfFile:@「TM-1P2.png」]; 這一行︰ _image = [UIImage imageNamed:@「TM-1P2.png」]; –

相關問題