2011-01-31 110 views
10

我有以下代碼:的UIImageView不顯示圖片

@interface NeighborProfileViewController : UIViewController { 
    UIImageView * profPic; 
    UITextView * text; 
    UIButton * buzz; 
    NSString * uid; 
    NSMutableData *responseData; 
    NSDictionary * results; 
} 

@property (nonatomic, retain) IBOutlet UIImageView * profPic; 
@property (nonatomic, retain) IBOutlet UITextView * text; 
@property (nonatomic, retain) IBOutlet UIButton * buzz; 
@property (nonatomic, retain) NSString * uid; 

@end 

下面是我在viewDidLoad.m文件

@implementation NeighborProfileViewController 
@synthesize uid; 
@synthesize profPic; 
@synthesize buzz; 
@synthesize text; 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSURL *url = //some URL to the picture; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    UIImage *img = [[[UIImage alloc] initWithData:data] autorelease]; 
    self.profPic = [[UIImageView alloc] initWithImage: img]; 
} 

我想我有線了通過IBUIImageView正確。我有一個從UIImageView到文件所有者中的profPic的插座。我究竟做錯了什麼?

回答

13

如果您設置的默認圖像是在您撥打self.profPic = [UIImageView]後保持可見狀態,還是從屏幕上刪除? 我認爲這個問題發生在self.profPic發佈舊的圖像視圖,以用你剛纔創建的圖像替換它。舊的UIImageView實例以及您在IB中定義的所有屬性可能會自動從超級視圖中移除。這就是爲什麼你沒有看到下載的圖像。

如果使用IB創建UIImageView那麼你不希望創建一個新的ImageView並將其分配給profPic(它已經是一個完全實例化UIImageView)。嘗試撥打[profPic setImage:img];,這將只改變profPic imageview中的圖像。

+0

在幾個小時的時間裏,我的腦袋正在跳動。在設置圖像之前再次分配imageview。謝謝@thelaws – sErVerdevIL 2013-01-09 06:58:35

2

@Equinox使用

[profPic setImage:img]; 

代替

self.profPic = [[UIImageView alloc] initWithImage: img]; 

雖然使用自動釋放是不是一個問題在這裏。 你也可以做這樣的事情

UIImage *img = [[UIImage alloc] initWithData:data] ; 
    [profPic setImage:img]; 
    [img release]; 

的問題

self.profPic = [[UIImageView alloc] initWithImage: img]; 

是,你正在創建profPic其它的存儲空間,這意味着profPic將不再指向的UIImageView在IB任何更多