我有一个带有标签1,2,3,4 ...
的多个Imageview现在我想在Imageview上加载各自标签的图像。
例如:如果有6张图像视图,那么
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;
}
}
写完之后,图像会被下载(Log显示一些img
的编码值),但它没有应用于各自的imageview。
请提前帮助并致谢。
答案 0 :(得分:1)
尝试下面的
//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
});
答案 1 :(得分:0)
在您将分配的图像视图添加到父视图的位置,它应该仅在for循环内完成,每次将一个图像分配给图像视图,您需要将此图像视图添加到父视图查看以显示它,请检查。