7

我了解如何以编程方式从 URL 为我的应用程序加载图像,而不是将它们打包在应用程序中,但我该如何处理 1x 与 2x 问题?如果需要,我可以从外部源提供两个版本,但是在设置 UIImage 时如何处理?

4

1 回答 1

7

我很确定您不能以自动方式远程加载@2x 图像文件。您必须测试视网膜显示,然后获取适当的图像,如下所示:

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];
于 2011-06-19T20:09:43.640 回答