Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should retina/normal images be handled when loading from URL?

I understand how to programmatically load images for my app from a URL instead of packaging them within the app but how do I handle the 1x vs 2x issue? I can serve both versions from the external source if need be but how do I handle that when setting the UIImage?

like image 842
markdorison Avatar asked Jun 19 '11 18:06

markdorison


1 Answers

I'm pretty sure you cannot load @2x image files remotely in an automated way. You will have to test for the retina display and then get the appropriate image(s), like so:

UIImage *image;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){
  // @2x
  NSURL *imageURL = [NSURL URLWithString:@"http://www.example.com/images/[email protected]"];
  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];
like image 107
Dan Sandland Avatar answered Oct 07 '22 07:10

Dan Sandland