iOS中如何从URL加载高清图片?

4
我希望从Web服务中加载不同尺寸(1x、2x和3x)的图标,但是当没有以x2结尾的图像名称时,ios总是将其用作1x。 我已经找到了一些答案,应该提供一个带有imagename@2x.png的url,以便iOS可以将其识别为Retina图像,但是如何正确地处理3个尺寸的远程图像?在我的情况下,我有一个可以提供所有尺寸图像的Web服务。 我想通过url指定设备的正确尺寸。例如:

http://example.com/x2/image.png

或者

http://example.com/x3/image.png

通常我会使用Images.xcassets提供不同尺寸的图片,但这一次我想要远程加载图片。我该如何检查使用设备所需的正确尺寸?我应该询问显示分辨率(或iPhone类型)以确定应该加载哪个图像吗?
另外,我该如何告诉UIImageView从此URL获取图像?

http://example.com/x3/image.png

这里的3x是指三倍(不要将其渲染为三倍大小)吗?

提前感谢。

4个回答

3

使用 [UIScreen mainScreen].scale 知道需要的尺寸,然后使用此 API 加载图像:+ (nullable UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale。您可以指定图像的比例。


0

尝试使用

[UIScreen mainScreen].scale;

确定屏幕的比例。

在 Swift 中:

 UIScreen.mainScreen().scale

或者使用以下代码:

if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0)){
     // Retina display
}else{
    // non-Retina display
}

0

你可以定义一些宏,例如:

    #define iPad    UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

    #define iPhone5OrBelow ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height < 667)

    #define iPhone6 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 667)

    #define iPhone6Plus ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 736)

然后您可以检查任何这样的宏,例如:

if (iPhone6 || iPhone5OrBelow)
{
 // use @2ximage.png
}
else 
{
 // use @3ximage.png
}

希望这能有所帮助。


-3
You can check for iPhone.

For Ex :
if(iPHone5 || iPhone5S || iPhone6 || iPhone6S){
// load 2x images.
  use this link http://example.com/x2/image.png
  yourImgView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:yourLinkfor2ximage]];

  }
  else if(iPhone6+ || iPhone6+S ){
  //use this link http://example.com/x3/image.png
    yourImgView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:yourLinkfor3ximage]];
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接