简体   繁体   中英

UIImage from Mutablearray is not displaying

  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
{
}

But the problem is the image is not displating when i tried to log...

NSLog(@"Our Image is= %@", _thumb2 ); is giving me NULL... help me to fix this...

First, you have a logic error in your getImage method:

- (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
}

If there is no cached image available, then your getImage call is going to return nil , which of course will not display correctly.

Now I don't know the exact specifics of how SDWebImageManager is intended to be used, but from your code it looks like it will almost certainly perform an asynchronous download of the requested image if a local copy isn't immediately available in the cache. This means that you cannot call getImage and expect to immediately get back an image that you can display.

You have a couple of options for fixing this:

  1. Implement that webImageManager:didFinishWithImage: method that you've got there to handle the image that the image-manager downloads asynchronously, and restructure your code to work with it.

  2. Get rid of the SDWebImageManager and use a synchronous request to download the image if it is not in the cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM