How to handle retina / normal images when loading from url?

I understand how to programmatically load images for my application from a URL instead of packing them in the application, but how can I deal with the 1x vs 2x problem? I can serve both versions from an external source if necessary, but how can I handle this when setting up UIImage?

+7
source share
1 answer

I am sure that you cannot upload 2x2 image files remotely in an automatic way. You will need to check the retina screen and then get the corresponding image (s), for example:

UIImage *image; if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){ // @2x NSURL *imageURL = [NSURL URLWithString:@"http://www.example.com/images/ yourImage@2x.png "]; NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; image = [UIImage imageWithData:imageData]; } else { // @1x NSURL *imageURL = [NSURL URLWithString:@"http://www.example.com/images/yourImage.png"]; NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; image = [UIImage imageWithData:imageData]; } UIImageView *yourImageView = [[UIImageView alloc] initWithImage:image]; 
+7
source

All Articles