开发者

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

I understand how to programmatically load images for my app from a URL ins开发者_如何学运维tead 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/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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜