我有以下代码:
@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/