来自URL的iOS图像大小?

来自URL的iOS图像大小?,ios,objective-c,iphone,swift,Ios,Objective C,Iphone,Swift,我想从Web服务加载一些不同大小(1x、2x和3x)的图标,但是,当没有末尾带有x2的图像名称时,ios总是将其用作1x 我已经找到了一些答案,你应该提供一个urlimagename@2x.png-这样iOS就可以将is识别为视网膜图像,但对于3种尺寸的远程图像,该如何进行校正 就我而言,我有一个Web服务,它可以提供各种大小的图像。我想用url指定设备的正确大小 例如: 或 通常我会使用Images.xcsets提供所有不同的大小,但这次我想远程加载图像。如何检查所用设备的正确尺寸?我是否

我想从Web服务加载一些不同大小(1x、2x和3x)的图标,但是,当没有末尾带有x2的图像名称时,ios总是将其用作1x

我已经找到了一些答案,你应该提供一个urlimagename@2x.png-这样iOS就可以将is识别为视网膜图像,但对于3种尺寸的远程图像,该如何进行校正

就我而言,我有一个Web服务,它可以提供各种大小的图像。我想用url指定设备的正确大小

例如:

通常我会使用Images.xcsets提供所有不同的大小,但这次我想远程加载图像。如何检查所用设备的正确尺寸?我是否应该询问显示器分辨率(或iPhone类型?)以检查应该加载哪个图像

我如何说UIImageView是来自此Url的图像:

是3x(所以不要渲染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]];
  }
[UIScreen mainScreen].scale;
确定屏幕的比例

迅速:

或使用以下代码:

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

使用
[UIScreen Main Screen]。缩放
以了解您需要的尺寸。然后使用以下API加载图像:
+(nullable UIImage*)imageWithData:(NSData*)数据比例:(CGFloat)比例
。您可以指定图像的比例。

您可以定义一些宏,例如

    #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)
我希望这有帮助

    #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
}