Search code examples
iosasynchronousuiimageviewlazy-loading

SDWebImage - Image from url didn't replace placeholder image after it's finished download


I'm using SDWebImage to download and cache images in an UIImageView asynchronously but I'm facing some problems.

In my project I create a pin marker which has a dialogue box that have UIImageView in it.

The problem that I face is the image from url didn't replace place holder image after it's finished download. The first click on pin marker will always display the place holder image.

The image from url will only display after I click pin marker for the second time.

I want to display image from the url in the first click what should I do?

here is my code

    -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{
PlaceData *currentMarker = [self findDataByMarker:marker];

UIView *dialogView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 261, 95)];

// Create background image
UIImageView *dialogBackground = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"MapDialog.png"]];
[dialogView addSubview:dialogBackground];

UIImageView * thumbnail = [[UIImageView alloc] init];

[thumbnail sd_setImageWithURL:[NSURL URLWithString:currentMarker.thumbnail] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageRefreshCached];

NSLog(@"URL %@", currentMarker.thumbnail);
NSLog(@"thumbnail %@",thumbnail);



CGRect tmpFrame = thumbnail.frame;
tmpFrame.origin.x = 15;
tmpFrame.origin.y = 14;
tmpFrame.size.width = 55;
tmpFrame.size.height = 55;
thumbnail.frame = tmpFrame;
thumbnail.contentMode = UIViewContentModeScaleToFill;
[dialogView addSubview:thumbnail];

return dialogView;

PS. Please help me solve this problem and sorry for my english.


Solution

  • Please try with this code

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [thumbnail sd_setImageWithURL:[NSURL URLWithString:currentMarker.thumbnail] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageRefreshCached];
    });