简体   繁体   English

无法在资产目录中读取Swift图像

[英]Swift Images cannot be read in Asset Catalog

I'm trying to save some images into my bundle. 我正在尝试将一些图像保存到我的捆绑包中。 The images reads just fine when I save them in the main bundle. 当我将图像保存在主捆绑包中时,这些图像读得很好。 in main bundle 在主要捆绑中

However, if I place them into the asset catalog, they can no longer be read. 但是,如果我将它们放入资产目录中,将无法再读取它们。 In asset catelog 在资产类别中

Below are the codes I use to access them. 以下是我用来访问它们的代码。 If I save the pictures inside the asset catalog, the for item in items loop doesn't pick up any of the pictures. 如果我将图片保存在资产目录中,则for item in items循环不会拾取任何图片。

override func viewDidLoad() {
    super.viewDidLoad()

    let fm = FileManager.default
    let path = Bundle.main.resourcePath!
    let items = try! fm.contentsOfDirectory(atPath: path)

    for item in items {
        if item.hasPrefix("nssl") {
            pictures.append(item)
        }
    }
}

There's no documented way to iterate through the images contained in an xcasset file. 没有记录的方式来遍历xcasset文件中包含的图像。 At build time the assets get compiled to a .car file, but the only way to read images out of the asset is through UIImaged(named:) . 在构建时,资产被编译为.car文件,但是从资产中读取图像的唯一方法是通过UIImaged(named:)

If you need to be able to iterate through some resources in this way, it's best to store them in a simple folder inside your bundle. 如果您需要以这种方式遍历某些资源,则最好将它们存储在包内的简单文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM