xcode 带有 xcassets 的 iPhone 4" 屏幕无法拉出正确的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19056426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:56:54  来源:igfitidea点击:

iPhone 4" screen with xcassets not pulling correct image

iphoneiosxcodeios7xcode5

提问by Joel Bell

I am trying to implement using Images.xcassets into a project I am working on. From what I understand I can just put all the different sized images for different devices in there and then call [UIImage imageNamed:@"name_of_image_set"] and it will return the correct image for the device I am working on.

我正在尝试将 Images.xcassets 用于我正在处理的项目中。据我所知,我可以将不同设备的所有不同大小的图像放在那里,然后调用 [UIImage imageNamed:@"name_of_image_set"] 它将为我正在处理的设备返回正确的图像。

It seems to be pulling the correct image for everything except the iPhone 5/5s/5c with the 4" screen. For that screen size it gives me the image for the @2x iPhone with the 3.5" screen.

除了带有 4" 屏幕的 iPhone 5/5s/5c 之外,它似乎正在为所有内容提取正确的图像。对于该屏幕尺寸,它为我提供了带有 3.5" 屏幕的 @2x iPhone 的图像。

Image of config in Images.xcassets

Images.xcassets 中的配置图像

Here is the json that is included in the folder with the images.

这是包含在带有图像的文件夹中的 json。

{
  "images" : [
    {
      "idiom" : "iphone",
      "scale" : "1x",
      "filename" : "bg.png"
    },
    {
      "idiom" : "iphone",
      "scale" : "2x",
      "filename" : "[email protected]"
    },
    {
      "idiom" : "iphone",
      "filename" : "[email protected]",
      "subtype" : "retina4",
      "scale" : "2x"
    },
    {
      "idiom" : "ipad",
      "scale" : "1x",
      "filename" : "bg~ipad.png"
    },
    {
      "idiom" : "ipad",
      "scale" : "2x",
      "filename" : "bg@2x~ipad.png"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

Everything seems to be in order, am I just misunderstanding how xcassets are supposed to work?

一切似乎都井井有条,我只是误解了 xcassets 应该如何工作吗?

Thanks

谢谢

Joel Bell

乔尔·贝尔

采纳答案by ale0xB

I came across this problem and the issue seems to be targeting iOS versions lower than 7.0 The solution for me was to create a separate image set with a single @2x image on it and instantiate the correct one programmatically by detecting the iPhone screen size in code like done here

我遇到了这个问题,问题似乎是针对低于 7.0 的 iOS 版本对我来说,解决方案是创建一个单独的图像集,上面有一个 @2x 图像,并通过在代码中检测 iPhone 屏幕大小以编程方式实例化正确的图像喜欢在这里完成

Related: Why isn't my Asset Catalog returning R4 images?

相关: 为什么我的资产目录不返回 R4 图像?

回答by Charles

I had the same issue but only in ios7 and I load the images programmatically, but it should be the same problem.

我有同样的问题,但只在 ios7 中,我以编程方式加载图像,但应该是同样的问题。

In my viewDidLoad I added:

在我的 viewDidLoad 中,我添加了:

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { // if iOS 7
    self.edgesForExtendedLayout = UIRectEdgeNone; //layout adjustements
}

It basically just recognizes iOS7 and applies some layout adjustements. After I added this code, the correct picture got selected. Finally I load my image, which you don't have to do:

它基本上只识别 iOS7 并应用一些布局调整。添加此代码后,选择了正确的图片。最后我加载我的图像,你不必这样做:

 [productview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];

Found this answer also here in stackoverflow, but didn't find it anymore.

在stackoverflow中也在这里找到了这个答案,但再也找不到了。