【问题标题】:Use launch image set on UIImageView使用 UIImageView 上设置的启动图像
【发布时间】:2023-03-17 03:52:02
【问题描述】:

我正在为我的应用创建说明屏幕,我必须在其中放置全屏大小的图像。为此,我必须考虑所有屏幕尺寸,因此我在 AssetCatalog 中创建了一个启动图像集。
在视图中,我有UIImageView。在为UIImageView 设置图像时,我得到一个白屏。我猜无法在UIImageView 上设置启动图像。
这里最好的方法是什么?
注意:如果需要,请根据最新的命名约定(包括 iPhone X)分享您的答案。

【问题讨论】:

  • 请检查这个问题,你可能会在这里找到答案stackoverflow.com/questions/19410066/…
  • @AsadAli :链接不太清楚。我什么都试过了。
  • 你猜对了,苹果不允许我们将 Launch 图像集用于图像视图,可能的方法是创建一个普通的图像资源并使用它。

标签: ios uiimageview asset-catalog


【解决方案1】:

启动屏幕就像一个普通的故事板。您可以添加图像视图控制器、文本标签或其他 UIKit 控件。

出于各种原因,我建议不要在启动屏幕上使用静态图像。

  • 无法本地化。
  • 它可能会根据设备的屏幕分辨率和方向而变形。

相反,将启动屏幕视为任何其他视图控制器。添加控件、图像和静态文本以及适当的布局约束。这种方法将为您提供更大的灵活性和更好的扩展性。

如果您真的想添加静态图像,请将 UIImageView 控件添加到启动视图控制器并将其尺寸设置为视图的范围。然后,将图像分配给图像视图控件。

【讨论】:

  • "启动屏幕就像一个普通的故事板" - 除了它不能与自定义类或任何代码相关联。
  • 你的最后一个建议是我正在尝试的。只有命名约定是我错的地方。
【解决方案2】:

您不能直接使用启动图像。我找到了解决方法:

func getLaunchImageName() -> String? {
    // Get All PNG Images from the App Bundle
    let allPngImageNames = Bundle.main.paths(forResourcesOfType: "png", inDirectory: nil)
    // The Launch Images have a naming convention and it has a prefix 'LaunchImage'
    let expression = "SELF contains '\("LaunchImage")'"
    // Filter the Array to get the Images with the prefix 'LaunchImage'
    let res = (allPngImageNames as NSArray).filtered(using: NSPredicate(format: expression))
    var launchImageName: String = ""
    // Now you have to find the image for the Current Device and Orientation
    for launchImage in res {
        do {
            if let img = UIImage(named: (launchImage as? String ?? "")) {
                // Has image same scale and dimensions as our current device's screen?
                if img.scale == UIScreen.main.scale && (img.size.equalTo(UIScreen.main.bounds.size)) {
                    // The image with the Current Screen Resolution
                    launchImageName = launchImage as? String ?? ""
                    // You can store this image name somewhere, I have stored it in UserDefaults to use in the app anywhere
                    UserDefaults.standard.set(launchImageName, forKey: "launchImageName")
                    break
                }
            }
        }
    }
    return launchImageName
}

【讨论】:

    【解决方案3】:

    我认为您创建了一个启动屏幕并在其中使用了 imageview。

    如果您使用它,那么您必须将图像放在主包中,而不是在 Assets 中,因为资产不会在启动时调用。使用主包图像。

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      在您的LaunchVC 中获取完整尺寸的UIImageView

      1. 将您的图像集作为Resources-> Images 放在一个资源文件夹中。
      2. 将所有图像放入AssetCatalog,就像您已经完成的那样。为此,您必须具有 1x、2x、3x 图像大小才能在 AssetCatalog 中显示图像。

      我认为您在bundleAsset Catlog 中都不可能有图像,并且图像名称没有拼写错误,但您仍然没有得到图像。

      【讨论】:

        【解决方案5】:

        在不知道图像的情况下,很难分辨。 一种方法是简单地使用 SVG 文件(或者在 iOS 中使用 PDF 矢量文件),然后使用比例设置为 single scale 的图像资源。 如果您的图像由于文本或其他内容而无法“缩放”,您可能需要考虑在 iOS 中使用标签和图像视图自行构建它。

        【讨论】:

          【解决方案6】:

          我认为您需要新的启动屏幕来显示您的启动图像。为此,您创建新的故事板并将默认启动屏幕更改为目标中的新启动屏幕。看到这张图片

          在启动屏幕文件更改中选择您的新启动屏幕。这个新的启动屏幕可以从资产访问您的图像文件。

          在很多情况下,如果我们想在闪屏中显示动画,我们会采用这种方法。试试这个方法,它可以解决你的问题。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-01-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-19
            相关资源
            最近更新 更多