Downloading normal image Vs retina device image (2

2019-03-09 00:40发布

问题:

When we need to download an image from some URL and show it on two kinds of devices -- Retina (with 2x image) and regular device -- Should we have two different image URLs to handle this?

For the images in the resource bundle we are keeping both xyz.png and xyz@2x.png and its working fine.

For images we are fetching from server do we need to have separate image URLs for both these kind of images and cache them locally with the same naming convention (xyz.png and xyz@2x.png)?

Please throw some light here.

回答1:

You can check if the device has a high-resolution retina display and based on that download a different image. Don't bother for photos and stuff that you'd scale anyway for interface size.
You can create the scaled version of the downloaded image with

UIImage *image = //download...
image=[UIImage imageWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp];

Keep in mind that a scaled 100x100 image will become a 50x50 points image (with 2.0 scale).

Check first if you have a retina display

BOOL retina = NO;
if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    retina = [[UIScreen mainScreen] scale] == 2.0 ? YES : NO;


回答2:

CGFloat screenScale = [UIScreen mainScreen].
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:myUrl] scale:screenScale];