Loading Retina Image Using SDWebImage

In a previous article I discussed the problem with loading retina images from a URL. SDWebImage is a popular library that allows asynchronous loading of images and caching. Unfortunately, it does not provide very good support for loading retina images.

If a URL ends with “@2x”, SDWebImage will do the processing for retina image (I have not tried that). But, it is not a very practical solution. To solve the problem, you will need to scale the image from a completion block:

- (void) setImage: (UIImageView*) iv URL: (NSSTring*) url {
    __weak UIImageView *wImageView = iv;

    [iv setImageWithURL:[NSURL URLWithString:url]
        placeholderImage:[UIImage imageNamed:@"noimage.gif"] 
        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            CGFloat scale = [UIScreen mainScreen].scale;

            if (scale > 1.0) {
                image = [UIImage imageWithCGImage:[image CGImage] 
                    scale:[UIScreen mainScreen].scale 
                    orientation:UIImageOrientationUp];

                NSLog(@"Scaling image");

                wImageView.image = image;
            }
    }];
}

One thought on “Loading Retina Image Using SDWebImage

  1. Updated for SDWebImage 3.7.2:

    – (void) setImage:(UIImageView*) iv URL:(NSString *) url {
    __weak UIImageView *wImageView = iv;

    [iv sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@”transparent-placeholder”] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    CGFloat scale = [UIScreen mainScreen].scale;

    if (scale > 1.0) {
    image = [UIImage imageWithCGImage:[image CGImage]
    scale:[UIScreen mainScreen].scale
    orientation:UIImageOrientationUp];

    // NSLog(@”Scaling image”);

    wImageView.image = image;
    }
    }];
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s