问题
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?
回答1:
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/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];
来源:https://stackoverflow.com/questions/6404410/how-should-retina-normal-images-be-handled-when-loading-from-url