我有一个带有标签 1,2,3,4...的多个 Imageview...
现在我想在该 Imageviews 上加载带有各自标签的图像。
例如:如果有 6 个 ImageView ,则
for(int i=0;i<6;i++)
{
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xpos+10,20, 75, 45)];
xpos+=100;
imgView.tag=i;
if ([[msg_array objectAtIndex:i] valueForKey:@"Image"]) //Image is cached then assign it
{
imgView.image=[[msg_array objectAtIndex:i] valueForKey:@"Image"];
}
else //Image is not there download it..
{
if ([[msg_array objectAtIndex:i] valueForKey:@"Image"] length]!=0)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self downloadImage_3:[[msg_array objectAtIndex:i] valueForKey:@"Merchant_SmallImage"] AtIndex:i];
});
}
}
}
//Downloads Images asynchronously..
-(void)downloadImage_3:(NSString*)ImageUrl AtIndex:(int)i{
if ([ImageUrl length]!=0)
{
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ImageUrl]]];
NSLog(@"img = %@",img);
imgView.image=img;
}
}
写完这篇文章后,图像正在被下载(日志显示了 img
的一些编码值),但它没有应用到其各自的 ImageView 上。
请帮助并提前致谢。
最佳答案
尝试以下方法
//change your download method like
-(void)downloadImage_3:(NSString*)ImageUrl forImageView :(UIImageView *)imgView
{
if ([ImageUrl length]!=0)
{
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ImageUrl]]];
dispatch_async(dispatch_get_main_queue(), ^{
[imgView setImage:img];
});
}
}
//call your download method like below
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self downloadImage_3:[[msg_array objectAtIndex:i] valueForKey:@"Merchant_SmallImage"] forImageView:imgView]; //pass your ImageView here
});
关于ios - 使用其标签将图像添加到 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658314/