2013-06-05 44 views
1

我試圖從Xcode中的ViewController上的文件顯示圖像。我有我的ViewController.h:顯示UIImage

UIImageView *image; 
@property(nonatomic,retain) IBOutlet UIImage* image ; 

ViewController.m

- (id)initWithImage:(UIImage *)image 
{ 
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nameoffile.jpg"]]; 

    self.image = image; 

    [image release]; 
} 

,但沒有運氣。它不會讓我把我的UIImage實例鏈接到xib中。

+3

你應該在廈門國際銀行創建的UIImageView例如,這將已經包含必要的圖像。 –

回答

0

由於UIImageView是當筆尖文件被加載創建的,所以沒有必要讓一個實例變量,所以從你的頭文件中刪除:

的UIImageView *圖像;

接下來確保您的UIImageView插座實際上已正確連接到NIB文件中的圖像視圖。你會看到一個實心圓在連接時(這是從開始iOS開發例如,使用ARC,所以忽略weak關鍵字):

enter image description here

最後,你應該能夠設置圖像視圖的在viewDidLoad方法圖像(有沒有必要使用該UIImage實例變量要麼,因爲它會在UIImageView內保留):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    imageView.image = [UIImage imageNamed:@"nameoffile.jpg"]; 
} 
+0

嗯,它給了我沒有變量聲明的錯誤。 –

+0

@AndreaF你不需要'UIImageView'或'UIImage'的變量。圖像視圖位於NIB內,因此您只需要一個連接('IBOutlet'),僅此而已,並且'UIImage'作爲圖像視圖的一部分保留,因此不需要將其存儲爲實例變量。 – trojanfoe

+0

@trojanfoe他正在爲UIImage製作IBOutlet而不是用於UIImageView。 UIImage無法連接到UIImageView在NIB –

1

嘗試使用這一個。你知道你不能在UIImage上獲得IBOutlet。所以你需要寫UIImageView而不是UIImage。

@property(nonatomic,retain) IBOutlet UIImageView* image ; 
0

檢查您的代碼的錯誤。

@property(nonatomic,retain) UIImageView *imageView; 
@property(nonatomic,retain)UIImage* image ; 

- (id)initWithImage:(UIImage *)image 
{ 
    _imageView = [[UIImageView alloc] initWithImage:image]; 
    _imageView.frame=self.view.frame; 
    [self.view addSubView:_imageView]; 

    [image release]; 
} 
+0

謝謝,但不知道加載文件。 _imageView = [[UIImageView alloc] initWithImage:_image:@「file.jpg」]; –

+0

imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@「file.jpg」]]; – 2013-06-05 09:53:47