iphone - UIImageView 不显示图像

标签 iphone objective-c

我有以下代码:

@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 释放旧 ImageView 以将其替换为您刚刚创建的图像时,就会出现此问题。旧的 UIImageView 实例,以及您在 IB 中定义的所有属性,可能会自动从 super View 中删除。这就是您看不到下载的图像的原因。

如果您使用 IB 创建了 UIImageView,那么您不想创建一个新的 ImageView 并将其分配给 profPic(这已经是一个完全实例化的 UIImageView)。尝试调用 [profPic setImage:img]; 这只会更改 profPic imageview 中的图像。

关于iphone - UIImageView 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4847560/

相关文章:

iphone - iOS 5.0.1:如何为iCloud验证文件夹是否标记为“不备份”?

ios - CALayer 层未自动释放

iOS:URL 字符串中的 & 号问题

iPhone sdk paypal 集成 API

ios - 使用 MTKView 显示 JPEG 图片

iphone - 将 iPad/iPhone 应用程序与 Rails 应用程序集成

iOS:致命异常:NSRangeException *** -[__NSArrayM objectAtIndex:]:索引 1 超出范围 [0 .. 0]

未连接到计算机时的 iPhone 内存泄漏监控

ios - 从 Objective C 中的 Swift 类继承

iphone - Xcode,只检索带有字母 A 的地址簿姓氏(依此类推)