【问题标题】:iOS Image from URL Retina Size?来自 URL 视网膜大小的 iOS 图像?
【发布时间】:2023-03-03 13:17:01
【问题描述】:

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

我已经找到了一些答案,您应该提供一个带有 imagename@2x.png 的 url - 以便 iOS 可以识别为 Retina Image,但是对于 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

是 3 倍(所以不要渲染 3 倍大)?

提前致谢

【问题讨论】:

    标签: ios objective-c iphone swift


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      尝试使用

      [UIScreen mainScreen].scale;
      

      确定屏幕的比例。

      迅速:

       UIScreen.mainScreen().scale
      

      或者使用下面的代码:

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

      【讨论】:

        【解决方案3】:

        您可以定义一些宏,例如,

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

        我希望这会有所帮助。

        【讨论】:

          【解决方案4】:
          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]];
            }
          

          【讨论】:

            猜你喜欢
            • 2012-07-14
            • 2014-11-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多