资产目录UIImage根据设备和方向错误地调整大小

时间:2014-03-09 10:15:20

标签: ios objective-c uiimage orientation asset-catalog

我正在尝试向我在启动时在我的应用中显示的第一个视图显示背景图像,该视图与LaunchImage显示的图像相同。

我已正确设置LaunchImage资产目录,并在启动时正确显示。我正在使用iPad模拟器并将其置于横向。

然而,我的背景图像不仅方向错误,而且尺寸错误。

考虑以下代码...

[self.backgroundImageView setImage:[UIImage imageNamed:@"LaunchImage"]];

NSLog(@"Image size should be: %@", [[UIScreen mainScreen] scale] == 1 ? @"1024x768":@"2048x1136");
NSLog(@"Image size is:%@", NSStringFromCGSize(self.backgroundImageView.image.size));

第一个NSLog正确地指出1024x768。

然而,第二个NSLog状态为320x480。这只是资产目录中的第一个图像,即iPhone LaunchImage。

这里有什么想法吗?

更新:

我在网上看到了很多关于图像命名约定的答案,下面的第一个答案足以列出所有这些。对于需要这些的人来说,这是一个很好的列表。

但是,资产目录的要点是存储每个设备的所有图像,iOS6或7,视网膜或非视网膜,以及哪个方向。如果每次显示图像时仍然需要检查设备,方向,是否是视网膜,那么资产目录的重点是什么?我认为那是他们的原因之一。

我应该注意到这是有效的,因为我希望在我的应用程序中过去工作。然后我更改了资产目录中的图像,所有图像仍然有效。然而,我决定将图像更改回原件,这就是一切都破裂的地方。我已经尝试清理构建甚至删除派生数据并重新启动xCode。不行。

我确实在资产目录中有正确的文件。但是你会注意到它们没有保存文件名,如下所述,Alexander Kostiev的回答如你所料。相反,它们以尺寸命名。但是他们在json中被正确引用,我现在将在下一篇文章中发表...

{
  "images" : [
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "AppImage@2x320x480.png",
      "scale" : "2x"
    },
    {
      "extent" : "full-screen",
      "idiom" : "iphone",
      "subtype" : "retina4",
      "filename" : "AppImage@2x320x568.png",
      "minimum-system-version" : "7.0",
      "orientation" : "portrait",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "AppImage768x1024.png",
      "scale" : "1x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "AppImage1024x768.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "AppImage@2x768x1024.png",
      "scale" : "2x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "full-screen",
      "minimum-system-version" : "7.0",
      "filename" : "AppImage@2x1024x768.png",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "AppImage320x480.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "AppImage@2x320x480-1.png",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "iphone",
      "extent" : "full-screen",
      "filename" : "AppImage@2x320x568-1.png",
      "subtype" : "retina4",
      "scale" : "2x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "to-status-bar",
      "filename" : "AppImage768x1004.png",
      "scale" : "1x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "to-status-bar",
      "filename" : "AppImage1024x748.png",
      "scale" : "1x"
    },
    {
      "orientation" : "portrait",
      "idiom" : "ipad",
      "extent" : "to-status-bar",
      "filename" : "AppImage@2x768x1004.png",
      "scale" : "2x"
    },
    {
      "orientation" : "landscape",
      "idiom" : "ipad",
      "extent" : "to-status-bar",
      "filename" : "AppImage@2x1024x748.png",
      "scale" : "2x"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

正如您在顶部的第四项中所看到的那样,iPad的json没有以纵向布局显示的视网膜。我已经确认此文件存在于asset-catalog目录中,实际上它是我在预览中查看时的正确图像。

还有什么想法?

更新2:

我现在也尝试添加名为background-Landscape~ipad.png和background-Portrait~ipad.png的文件。我现在使用以下内容加载图像...

    [self.backgroundImageView setImage:[UIImage imageNamed:@"background"]];

我尝试过background和background.png。但是没有显示图像,image.size为0,0。即。 UIImage imageNamed给了我一个零图像。

我现在完全失去了。

1 个答案:

答案 0 :(得分:0)

这是LaunchImage的完整列表:

  • LaunchImage-568h@2x.png
  • LaunchImage-700-568h@2x.png
  • LaunchImage-700-Landscape@2x.png
  • LaunchImage-700-Landscape.png
  • LaunchImage-700-Portrait@2x.png
  • LaunchImage-700-Portrait.png
  • LaunchImage-700@2x.png
  • LaunchImage-Landscape@2x.png
  • LaunchImage-Landscape.png
  • LaunchImage-Portrait@2x.png
  • LaunchImage-Portrait.png
  • LaunchImage.png
  • LaunchImage@2x.png

因此,您可能需要先检查方向并获得正确的图像。请考虑使用类似

的内容
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientation)) {
    imageName = @"LaunchImage-Portrait";
} else {
    imageName = @"LaunchImage-Landscape";
}

我们假设所有启动图像都在您的资产目录中正确设置