【问题标题】:Add Image to UIImageView with its tag将 Image 添加到 UIImageView 及其标签
【发布时间】:2014-01-06 15:23:29
【问题描述】:

我有一个带有标签 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 的一些编码值),但它没有应用于其各自的图像视图。

请帮助并提前致谢。

【问题讨论】:

  • 您是否将您的图像视图添加为视图的子视图?
  • 使用图片延迟加载从url加载图片

标签: ios iphone ipad uiimageview uiimage


【解决方案1】:

在将分配的图像视图添加到父视图的地方,它应该只在 for 循环内完成,每次将一个图像分配给图像视图时,您需要将此图像视图添加到父视图查看以使其显示,请检查。

【讨论】:

  • 如果我这样做,下载图像需要很多时间,我不想在主线程上执行下载。
【解决方案2】:

试试下面的

 //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
        });

【讨论】:

  • 我写了-(void)downloadImage_3:(NSString*) ImageUrl forImageView :(UIImageView *)imgVw{ NSLog(@"ImageUrl =%@", ImageUrl); if ([ImageUrl length]!=0) { UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:Path]]]; if (img) { [dicImages_msg setObject:img forKey:Path]; } imgView.image=[dicImages_msg valueForKey:Path]; } } ImageUrl 显示了 Url 的路径,但没有应用静止图像
  • 什么是Path??你用过[Path length]
  • 你为什么写这个imgView.image=[dicImages_msg valueForKey:Path]??
  • 当我写dispatch_async(dispatch_get_main_queue(), ^{ [imgView setImage:img]; }); 时,它的工作真棒,但图像的顺序不正确,有时第二张图像被应用到第四个 Imageview 或第三张图像被应用到第二个 Imageview。 :(
  • 然后检查你从哪里通过imageView 我的意思是[self downloadImage_3:[[msg_array objectAtIndex:i] valueForKey:@"Merchant_SmallImage"] forImageView:imgView];
猜你喜欢
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多