ios 设备上的启动屏幕中的 UIImageView 缺少图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32579150/
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-08-31 07:38:34  来源:igfitidea点击:

UIImageView missing images in Launch Screen on device

iosobjective-cuiimageviewuiimagelaunch-screen

提问by MFW

I have an app that supports iOS8 and later, built in Xcode 7 and I am using a XIB for a Launch Screen (I do not have launch images). The view contains a single UILabel with the app version, and 2 UIImageViews with images that are both present in Images.xcassets: A logo and a splash image.

我有一个支持 iOS8 及更高版本的应用程序,内置于 Xcode 7,我使用 XIB 作为启动屏幕(我没有启动图像)。该视图包含一个带有应用程序版本的 UILabel 和 2 个带有图像的 UIImageViews,它们都存在于 Images.xcassets 中:一个徽标和一个初始图像。

The UILabel and the logo image appear correctly when I launch the application, but the splash image does not if I run the app on an iPad Air 2 with iOS9. I have tested on an Air and a Mini running iOS8, and iOS9 simulators for iPad 2, iPad Air, and iPad Air 2 and the image appears correctly in all of those.

当我启动应用程序时,UILabel 和徽标图像正确显示,但如果我在带有 iOS9 的 iPad Air 2 上运行应用程序,则初始图像不会显示。我已经在运行 iOS8 的 Air 和 Mini 以及适用于 iPad 2、iPad Air 和 iPad Air 2 的 iOS9 模拟器上进行了测试,并且图像在所有模拟器中都能正确显示。

I ran some basic troubleshooting to see if I could figure out what is going on but I haven't been able to solve it and the only difference I can see between the image that's working and the one that's failing is whenI added it to the assets...

我进行了一些基本的故障排除,看看我是否能弄清楚发生了什么,但我无法解决它,我能看到的工作图像和失败图像之间的唯一区别是我将它添加到资产...

Here's a rundown of what I know:

以下是我所知道的概要:

  • The UIImageView for the splash image is in the correct place, at the correct size. I can tell this because I set its background color to green just to make sure. The view is there, but the image does not appear. So I'm assuming that the view is not to blame.

  • Setting the UIImageView for the splash image to also use the logo image makes the logo image appear in the correct place for the view. This also leads me to assume that the view is not to blame.

  • The UIImage that I am using in the splash image view is used elsewhere in the app and appears fine in those other views (the logo image is also used elsewhere in the app and appears fine). So I'm assuming that the image is valid and having it appear in other views is not a problem.

  • I've confirmed that the settings of the UIImages for the logo and splash in the xcassets file are the same. They are set to Universal, Any width and height, multiple scale factors, rendered as default. There is one difference - the logo has 1x, 2x and 3x scales while the splash image only has 1x and 2x, but I have also tried using UIImages with only 1x, and 1x and 2x values in the UIView and they work (if they were added to the project some time ago).

  • Adding another image of a different size or format (PNG and JPG) to my xcassets and using that UIImage in the UIImageView for the splash image also fails to display.

  • Adding another UIImageView to the XIB file and allocating it a UIImage that was already in the xcassets works, the image appears in the loading screen.

  • Copying and renaming the image files used for the logo and adding them to the project then using that UIImage in the splash view also fails to display.

  • I have tried cleaning the project, restarting the development machine, and deleting the app from the Air 2 and reinstalling it just in case that was a problem.

  • 初始图像的 UIImageView 位于正确的位置,大小正确。我可以这么说是因为我将它的背景颜色设置为绿色只是为了确定。视图在那里,但图像没有出现。所以我假设这种观点不应该受到指责。

  • 将初始图像的 UIImageView 设置为也使用徽标图像会使徽标图像出现在视图的正确位置。这也使我认为不应该责怪这种观点。

  • 我在初始图像视图中使用的 UIImage 在应用程序的其他地方使用,并且在其他视图中显示正常(徽标图像也在应用程序的其他地方使用并且显示正常)。所以我假设图像是有效的并且它出现在其他视图中不是问题。

  • 我已经确认 xcassets 文件中徽标和飞溅的 UIImages 设置是相同的。它们被设置为通用、任意宽度和高度、多个比例因子,默认呈现。有一个区别 - 徽标有 1x、2x 和 3x 比例,而初始图像只有 1x 和 2x,但我也尝试在 UIView 中使用 UIImages 仅具有 1x、1x 和 2x 值,并且它们可以工作(如果它们是前段时间添加到项目中)。

  • 将另一个不同大小或格式(PNG 和 JPG)的图像添加到我的 xcassets 并在 UIImageView 中使用该 UIImage 作为初始图像也无法显示。

  • 将另一个 UIImageView 添加到 XIB 文件并为其分配一个已经在 xcassets 中的 UIImage 工作,图像出现在加载屏幕中。

  • 复制和重命名用于徽标的图像文件并将它们添加到项目中,然后在初始视图中使用该 UIImage 也无法显示。

  • 我尝试清理项目,重新启动开发机器,并从 Air 2 中删除应用程序并重新安装,以防万一。

These last three steps lead me to believe that there's some issue with images added after a certain point in the project file's lifetime. While I updated to Xcode 7 yesterday, the splash image was originally added in Xcode 6, but the logo image (also added in Xcode 6) was added some months before.

最后三个步骤让我相信在项目文件生命周期的某个时间点之后添加的图像存在一些问题。虽然我昨天更新到 Xcode 7,但初始图像最初是在 Xcode 6 中添加的,但徽标图像(也在 Xcode 6 中添加)是在几个月前添加的。

I've looked over the json files for the logo image and splash image and they appear to have the same format. I've also trawled through the pbxproj file looking for differences and I can't see any.

我查看了徽标图像和初始图像的 json 文件,它们似乎具有相同的格式。我还浏览了 pbxproj 文件以寻找差异,但我看不到任何差异。

So I was wondering if anyone had any idea why the launch screen might not display these new images I'm adding on the Air 2 specifically? Other questions I've been reading through relating to images not appearing all seem to relate either to Launch Images, or to images in XIB files that have associated classes, neither of which seems relevant here.

所以我想知道是否有人知道为什么启动屏幕可能不会显示我专门在 Air 2 上添加的这些新图像?我一直在阅读的与未出现的图像相关的其他问题似乎都与启动图像或 XIB 文件中具有关联类的图像有关,这两者在这里似乎都不相关。

回答by Aral Balkan

Turn if off and then on again.

如果关闭然后再打开。

Seriously, restart the device — that's what fixed it for me.

说真的,重新启动设备 - 这就是为我修复它的原因。

Here's what didn't work:

这是无效的:

  • Cleaning DerivedData
  • Cleaning the project
  • Uninstalling the app from the device
  • Restarting Xcode
  • Restarting the computer
  • 清洗派生数据
  • 清理项目
  • 从设备卸载应用程序
  • 重启Xcode
  • 重新启动计算机

Older observations:

较早的观察:

Just like the others, it:

就像其他人一样,它:

  • Works fine in the Simulator
  • Used to work on the devices (iPhone 5S, iPad Air)
  • No code changes (verified via git reset --hard HEAD), yet stopped working.
  • 在模拟器中运行良好
  • 用于在设备上工作(iPhone 5S、iPad Air)
  • 没有代码更改(通过 git reset --hard HEAD 验证),但停止工作。

What I remember triggering the first build where it stopped working was unplugging the iPhone while it was runnning. (Not sure if it's related.)

我记得触发它停止工作的第一个构建是在运行时拔掉 iPhone。(不确定是否相关。)

However, the git reset not fixing it (combined with all the clean steps not working) tells me it this must be getting cached somewhere else. Quite possibly in one of the .gitignored files? (I'm using Git ignore file for Xcode projects)

但是, git reset 没有修复它(结合所有不工作的干净步骤)告诉我它必须被缓存在其他地方。很可能在 .gitignored 文件之一中?(我正在为 Xcode 项目使用Git 忽略文件

One workaround that worked for one UIImageView (in the UIView that has three) but not for the others was putting the image into an .xcassets file. Reverting to the plain image file did not undo the fix. (Really feels like a caching error.)

一种适用于一个 UIImageView(在具有三个 UIView 的 UIView 中)但不适用于其他人的解决方法是将图像放入 .xcassets 文件中。恢复到纯图像文件并没有撤消修复。(真的感觉像是缓存错误。)

UPDATE: See top?— restarting the device is what fixed it. I get the feeling the wrong bitmap data is being cached on the device itself.

更新:见顶部? - 重新启动设备是修复它的原因。我感觉设备本身缓存了错误的位图数据。

Radar: http://openradar.appspot.com/23534818

雷达:http: //openradar.appspot.com/23534818

回答by NikLanf

"The solution for me was simply to make sure that the "Clears Graphics Context" box is unchecked for the UIImageView in the storyboard. Just changing this fixed it for me." No other solutions worked for me. Got it from apple developer forum

“对我来说,解决方案只是确保未选中故事板中 UIImageView 的“清除图形上下文”框。只需更改即可为我修复它。” 没有其他解决方案对我有用。从苹果开发者论坛得到

enter image description here

在此处输入图片说明

回答by gbitaudeau

I had exactly the same issue on an iPhone 6.

我在 iPhone 6 上遇到了完全相同的问题。

The very strange fact is that I was able to use another image from my images.xcassets which had the same size, compression, color space ...

非常奇怪的事实是,我能够使用我的 images.xcassets 中的另一个图像,它具有相同的大小、压缩、色彩空间......

I'm not able to find why it doesn't work but I found a solution : Simply put your file outside your images.xcassets and use its full name (with extension) in Interface Builder

我无法找到为什么它不起作用,但我找到了一个解决方案:只需将您的文件放在 images.xcassets 之外并在 Interface Builder 中使用其全名(带扩展名)

splashScreen in storyboard sample

故事板示例中的闪屏

Hope this help !

希望这有帮助!

回答by mayankk2308

The following sequence of operations fixed the issue for me on Xcode 7.3.1 and iOS 9.3.4:

以下操作序列为我解决了 Xcode 7.3.1 和 iOS 9.3.4 上的问题:

  1. Clean Your Build (?+?+K)
  2. Close Xcode
  3. Hard Reset iOS Device (Power + Home Button reboot)
  4. Relaunch Xcode - let indexing finish and build on device once it is ready.
  1. 清理你的构建 (?+?+K)
  2. 关闭 Xcode
  3. 硬重置 iOS 设备(电源 + 主页按钮重启)
  4. 重新启动 Xcode - 让索引完成并在准备好后在设备上构建。

回答by andrewbuilder

Many other answers suggest rebooting devices and / or rebuilding and / or reinstalling is the solution.

许多其他答案建议重新启动设备和/或重建和/或重新安装是解决方案。

While these answers work for some of us, the reason these work is because the app is forced to "retake" or "rebuild" cached snapshots.

虽然这些答案对我们中的一些人有效,但这些有效的原因是因为应用程序被迫“重新拍摄”或“重建”缓存的快照。

Cached snapshots are used by apps to present images before supporting data is loaded into memory, including the asset catalogue that contains storyboard images.

在支持数据加载到内存中之前,应用程序使用缓存快照来呈现图像,包括包含故事板图像的资产目录。

This SO answerby @jaga provides the solution, but not the reason, as to why UIImageViews are missing in the Launch Screen on device (but not in the simulator).

@jaga 的这个 SO 回答提供了解决方案,但不是原因,为什么 UIImageViews 在设备上的启动屏幕中丢失(而不是在模拟器中)。

Background

背景

Recently I attempted to use a custom font in my "Launch Screen.storyboard" file.

最近我尝试在我的“Launch Screen.storyboard”文件中使用自定义字体。

The reason I could not became clear after reading this SO answerby @TimCamber, which prompted me to do a little more research and testing.

在阅读@TimCamber 的这个 SO 答案后,我无法弄清楚原因,这促使我做更多的研究和测试。

The reason that a custom font is not included in my Launch Screen Storyboard file is the same reason an image within an asset catalogue is not included...

自定义字体未包含在我的启动屏幕情节提要文件中的原因与未包含资产目录中的图像的原因相同...

The app prepares portrait & landscape snapshots of the app's Launch Screen from your Launch Screen Storyboard file and (currently) saves it in your app bundle here:

该应用程序从您的启动屏幕故事板文件准备应用程序启动屏幕的纵向和横向快照,并(当前)将其保存在您的应用程序包中:

  • /Library/Caches/Snapshots/<<app_bundle>>/and
  • /Library/Caches/Snapshots/<<app_bundle>>/downscaled/.
  • /Library/Caches/Snapshots/<<app_bundle>>/
  • /Library/Caches/Snapshots/<<app_bundle>>/downscaled/.

This process is complete before any supporting files are loaded, including any custom fonts and the asset catalogue.

在加载任何支持文件(包括任何自定义字体和资产目录)之前,此过程已完成。

Solution

解决方案

As mentioned above by following @jaga's solution, literally all that is required is to:

如上所述,通过遵循@jaga 的解决方案,实际上所需要做的就是:

  1. (old school) add an image file (.PNG) to your app's bundle (as you can see from the screenshot below I added the image file under the Supporting Files subdirectory in an Images subdirectory), and
  1. (老派)将图像文件 (.PNG) 添加到您的应用程序包中(如下面的屏幕截图所示,我在 Images 子目录的 Supporting Files 子目录下添加了图像文件),以及

screenshot of relevant directories in Project Navigator in Xcode

Xcode 中 Project Navigator 中相关目录的截图

  1. reference that image in your UIImageView object in the Launch Screen Storyboard file...
  1. 在启动屏幕故事板文件中的 UIImageView 对象中引用该图像...

screenshot of Image View section for UIImageView under Attributes Inspector in Xcode

Xcode 中 Attributes Inspector 下 UIImageView 的 Image View 部分的屏幕截图

回答by Bruce L

I was seeing the same problem. Rebooting the device (iPhone 6) and rebuilding the app to the device is the only thing that fixed it. I tried many other tips and suggestions but nothing else worked. Only reboot + rebuild helped.

我看到了同样的问题。重新启动设备 (iPhone 6) 并将应用程序重建到设备是唯一修复它的方法。我尝试了许多其他提示和建议,但没有其他方法奏效。只有重启+重建有帮助。

回答by Ke Yang

My solution @iOS 11.x:

我的解决方案@iOS 11.x:

  • Use .jpgimage instead of .png. I think image file size matters;
  • (Optionally) Put image file in project directory rather than .xcassets;
  • 使用.jpg图像而不是.png. 我认为图像文件大小很重要;
  • (可选)将图像文件放在项目目录而不是.xcassets;

回答by Petro Shm

I had the same for app installed from TestFlight. Delete app -> then restart iPhone -> then reinstall appworks for me. Just reinstall app or just restarting device didn't help.

我从 TestFlight 安装的应用程序也是如此。 删除应用程序 -> 然后重新启动 iPhone -> 然后重新安装应用程序对我有用。只是重新安装应用程序或只是重新启动设备没有帮助。

回答by Jakub Truhlá?

Have you tried turning the device off and on again? Do it.

您是否尝试关闭并重新打开设备?做吧。

回答by Jim Rhoades

I was having the same problem, but restarting the device didn't help. What worked for me was deleting the UIImageView from my launch storyboard and adding a new one.

我遇到了同样的问题,但重新启动设备没有帮助。对我有用的是从我的启动故事板中删除 UIImageView 并添加一个新的。