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?
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With