簡體   English   中英

UIImageView沒有顯示圖像

[英]UIImageView not showing Image

我有以下代碼:

@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

這是我在.m文件的viewDidLoad中所擁有的內容

@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];
}

我想我通過IB正確連接了UIImageView 我有一個從UIImageView到文件所有者的profPic的插座。 我究竟做錯了什么?

如果您設置的默認圖像是在調用self.profPic = [UIImageView]后保持可見,還是從屏幕上刪除? 我認為當self.profPic發布舊圖像視圖以將其替換為您剛剛創建的圖像視圖時會出現此問題。 舊的UIImageView實例以及您在IB中定義的所有屬性可能會自動從超級視圖中刪除。 這就是為什么你沒有看到下載的圖像。

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

@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將不再指向你的IB中的UIImageView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM