【问题标题】:Loading a UIImage from xcassets从 xcassets 加载 UIImage
【发布时间】:2016-08-15 15:37:10
【问题描述】:

我正在尝试从 Image.xcassets 文件夹加载启动图像,但无济于事。 SO上有其他answers(和this one)声称要回答它,但他们的主要解决方案是像这样简单地加载图像

UIImage *image =  [UIImage imageNamed:@"Default@2x"];

为我返回 nil。

文件名正确命名,项目设置为使用资产。

有谁知道我是怎么做的或者我做错了什么?

提前致谢。

编辑:

编辑 2:我的最终代码:

-(void) loadSplashImage{
if ([self isiPad]){
    self.imageViewSplash.image =  [UIImage imageNamed:@"Default-Portrait"];
}
else{
    if (self.view.frame.size.height == 480){
        self.imageViewSplash.image =  [UIImage imageNamed:@"Default"];
    }
    else if (self.view.frame.size.height == 568){
        self.imageViewSplash.image =  [UIImage imageNamed:@"Default-568h"];
    }
    else if (self.view.frame.size.height == 667){
        self.imageViewSplash.image =  [UIImage imageNamed:@"Default-667h"];
    }
}

}

请注意,它仅适用于纵向。

【问题讨论】:

标签: ios objective-c uiimage xcasset


【解决方案1】:

这是获取 LaunchImage 名称的方法。

Xcode 5 & Asset Catalog: How to reference the LaunchImage?

从 info.plist ([[NSBundle mainBundle] infoDictionary]) 获取图像名称

【讨论】:

    【解决方案2】:

    你应该有 2 个文件:

    (1) Default.png(或任何其他图像格式)- 非视网膜

    (2) Default@2x.png — 视网膜

    现在,要获取此图像,您不必在文件名末尾使用@2x。仅使用该图像资产的名称。

    【讨论】:

    • 感谢 NSPratik,但我仍然得到零。
    • 尝试使用“默认”以外的名称
    【解决方案3】:

    您不必以您的名字指定图像的大小。它将自动加载最适合运行应用程序的设备的大小。所以你的代码应该是。

    UIImage *image =  [UIImage imageNamed:@"Default"];
    

    其中默认值是来自xcassets 的资源名称,即您在左侧列表中看到的资源名称。

    【讨论】:

    • 谢谢果冻,但我仍然没有得到。
    • 发一张你的xcassets 的截图,让我们看看它的样子
    • 你必须把它们放在xcode中,因为如果你把它们放在一个文件夹里,它们是不会被拾取的。
    • 您的 xcode 项目中应该有一个 Assets.xcassets 文件。打开它,您的资源应该会进入。在此处查看有关资产目录的更多信息developer.apple.com/library/ios/recipes/…。
    • 在 XCode 中添加对它们的引用就可以了。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2012-06-14
    • 2017-10-08
    • 2021-08-29
    • 2015-12-26
    • 2020-05-17
    • 1970-01-01
    相关资源
    最近更新 更多