Ios 使用URL设置UIImageView
我正在UIImageView中使用以下设置图像:Ios 使用URL设置UIImageView,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,我正在UIImageView中使用以下设置图像: [self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"random_image"]]; 我能够在手机上看到预期的图像 但是,如果我使用不带占位符参数的方法 [self.imageView setImageWithURL:[NSURL URLWithString:urlString]]; 或者将
[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"random_image"]];
我能够在手机上看到预期的图像
但是,如果我使用不带占位符参数的方法
[self.imageView setImageWithURL:[NSURL URLWithString:urlString]];
或者将占位符图像传递为nil
[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
然后图像未设置,并且我的imageView为空。我知道占位符图像用于“表示适合临时显示在扩展UI中的资产的当前状态。”
我不明白它的缺失会对我产生怎样的影响。有任何指针吗?如果设置默认占位符图像,则imageview将获得宽度和夜间。否则,imageview帧可能为(0,0) 或 应使用NSURLConnection异步下载映像,并且只有在完全下载映像后,才应将其转换为UIImage并分配给映像视图 [NSURLConnection sendAsynchronousRequest:[NSURLReRequestWithURL:[NSURL URLWithString:imageUrl]]队列:[NSOperationQueue mainQueue]完成处理程序:^(NSURLRResponse*响应,NSData*数据,NSError*错误){
}]) 我发现了这个问题。我在UITableViewCell中添加了一个名为imageView的图像视图。显然,UITableViewCell已经有一个同名的属性。我将myImageView的名称更改为cellImageView,现在,我不再需要占位符参数了!话虽如此,我还是不明白屏幕后面发生了什么 我希望这些代码能帮助你
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:@“image URL”]]];
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell myimage] setImage:image];
}
});
});
检查图像视图大小可能其(0;0)占位符图像为图像视图提供了一个固有的大小,这有助于替换占位符范围内的实际图像。如果没有为图像视图提供高度/宽度约束,则其大小将为(0,0)。我已在序列图像板中使用非(0,0)声明了imageView。但你们是对的,代码中的帧大小是0,0。但是,即使我手动给出帧大小,图片也不会显示。还有其他方法来定义“内在尺寸”吗?谢谢你的帮助。我尝试使用CGRectMake方法添加框架,也尝试了您的第二种解决方案。没用。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:@“image URL”]]];
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell myimage] setImage:image];
}
});
});