来自Mutablearray的UIImage没有显示

时间:2011-08-07 11:52:20

标签: ios

  NSUInteger index, count = [delegate.thumbArr count];
  for (index = 0; index < count; index++) 
  {

     NSString *tPath = [[NSString alloc] init];
     tPath = [delegate.thumbArr objectAtIndex:index];
     NSURL *turl = [NSURL URLWithString:tPath];

     UIImage *_thumb =[self getImage:turl];

     // Adding images to the NSMutableArray TViews
     [TViews addObject:_thumb];

     UIImage *_thumb2 =[TViews objectAtIndex:index];
     NSLog(@"Our Image is= %@", _thumb2 );

     [thumbView.imageView setImage:_thumb2];


  }

- (UIImage*)getImage:(NSURL*)myurl
{


    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    UIImage *cachedImage = [manager imageWithURL:myurl];

    if(!cachedImage)
    {
        [manager downloadWithURL:myurl delegate:self];
    }

   else
    return cachedImage;

}

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
}

但问题是我试图登录时图像没有显示...

NSLog(@"Our Image is= %@", _thumb2 );给我NULL ...帮我解决这个问题......

1 个答案:

答案 0 :(得分:1)

首先,您的getImage方法存在逻辑错误:

- (UIImage*)getImage:(NSURL*)myurl {

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    UIImage *cachedImage = [manager imageWithURL:myurl];

    if(!cachedImage) {
        [manager downloadWithURL:myurl delegate:self];
    }
    else {
        return cachedImage;
    }

    //execution will get here if there is no cached image; the "returned" value will be nil
}

如果没有可用的缓存图片,那么您的getImage电话将返回nil,这当然无法正确显示。

现在我不知道如何使用SDWebImageManager的具体细节,但是从你的代码看,如果本地副本不是,它几乎肯定会执行所请求图像的异步下载。 t在缓存中立即可用。这意味着您无法调用getImage并希望立即获取可以显示的图像。

您有几种方法可以解决此问题:

  1. 实现您所拥有的webImageManager:didFinishWithImage:方法来处理图像管理器异步下载的图像,并重构您的代码以使用它。

  2. 摆脱SDWebImageManager并使用同步请求下载图片(如果图片不在缓存中)。