从捆绑包中的资源文件夹加载图像

时间:2022-02-22 00:25:45

I have a bundle with assets folder in it. I have read all the answers on stack about using UIImage(named: "drop_arrow", inBundle: bundle, compatibleWithTraitCollection: nil) (well, it's swift 3 equivalent)

我有一个包含assets文件夹的包。我已经阅读了有关使用UIImage的堆栈上的所有答案(命名为:“drop_arrow”,inBundle:bundle,compatibleWithTraitCollection:nil)(好吧,它快速等同于3)

path = Bundle.main.path(forResource: "LiveUI", ofType: "bundle")
if path != nil { // path with bundle is found
     let bundle: Bundle = Bundle.init(path: path!)! // bundle is there
     let image: UIImage? = UIImage(named: "HomePlayButton", in: bundle, compatibleWith: nil)
     // HomePlayButton exists in the bundle/asset folder
     // but image is nil
}

This is my project structure:

这是我的项目结构:

从捆绑包中的资源文件夹加载图像

Can you see any problem with the code/structure of the project?

你能看到项目代码/结构有什么问题吗?

UPDATE! ... The image is set for all resolutions as universal: 从捆绑包中的资源文件夹加载图像

UPDATE! ...图像为所有分辨率设置为通用:

2 个解决方案

#1


6  

Two things to check.

要检查两件事。

1) make sure your assets bundle has an Info.plist with a bundle identifier set in it. From your screenshot it doesn't look like this is the case.

1)确保您的资产包中包含一个Info.plist,其中包含一个包标识符。从截图中看,情况并非如此。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleIdentifier</key>
    <string>MyBundle</string>
</dict>
</plist>

2) Make sure your bundle has a compiled assets catalog in it. Should be named Assets.car. I don't think Xcode will just compile an Assets.xcassets folder inside a resource bundle (the bundle is effectively opaque to Xcode; likewise it wouldn't compile any .m source files you put in there). You may need a custom build step to copy the compiled Assets.car file into your assets bundle before the assets bundle is copied into the app bundle.

2)确保您的包中包含已编译的资产目录。应该命名为Assets.car。我不认为Xcode会在资源包内编译一个Assets.xcassets文件夹(该包对Xcode实际上是不透明的;同样它也不会编译你放在那里的任何.m源文件)。在将资产包复制到应用程序包之前,您可能需要自定义构建步骤将已编译的Assets.car文件复制到资产包中。

You can check these by finding your app bundle and right clicking on it then "Show Package Contents", then again on the contained bundle.

您可以通过找到您的应用程序包并右键单击它然后“显示包内容”,然后再次在包含的包上检查这些。

#2


0  

Just an idea, make sure the bundle and the asset catalog in the bundle have a Target Membership property assigned to the correct target you are trying to load the images with (Utilities pane -> Target Membership):

只是一个想法,确保捆绑包和捆绑包中的资产目录具有分配给您尝试加载图像的正确目标的目标成员资格属性(实用工具窗格 - >目标成员资格):

从捆绑包中的资源文件夹加载图像

It's possible that your target is able to find the bundle but not the asset catalog for some reason.

由于某种原因,您的目标可能无法找到捆绑包而非资产目录。

#1


6  

Two things to check.

要检查两件事。

1) make sure your assets bundle has an Info.plist with a bundle identifier set in it. From your screenshot it doesn't look like this is the case.

1)确保您的资产包中包含一个Info.plist,其中包含一个包标识符。从截图中看,情况并非如此。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleIdentifier</key>
    <string>MyBundle</string>
</dict>
</plist>

2) Make sure your bundle has a compiled assets catalog in it. Should be named Assets.car. I don't think Xcode will just compile an Assets.xcassets folder inside a resource bundle (the bundle is effectively opaque to Xcode; likewise it wouldn't compile any .m source files you put in there). You may need a custom build step to copy the compiled Assets.car file into your assets bundle before the assets bundle is copied into the app bundle.

2)确保您的包中包含已编译的资产目录。应该命名为Assets.car。我不认为Xcode会在资源包内编译一个Assets.xcassets文件夹(该包对Xcode实际上是不透明的;同样它也不会编译你放在那里的任何.m源文件)。在将资产包复制到应用程序包之前,您可能需要自定义构建步骤将已编译的Assets.car文件复制到资产包中。

You can check these by finding your app bundle and right clicking on it then "Show Package Contents", then again on the contained bundle.

您可以通过找到您的应用程序包并右键单击它然后“显示包内容”,然后再次在包含的包上检查这些。

#2


0  

Just an idea, make sure the bundle and the asset catalog in the bundle have a Target Membership property assigned to the correct target you are trying to load the images with (Utilities pane -> Target Membership):

只是一个想法,确保捆绑包和捆绑包中的资产目录具有分配给您尝试加载图像的正确目标的目标成员资格属性(实用工具窗格 - >目标成员资格):

从捆绑包中的资源文件夹加载图像

It's possible that your target is able to find the bundle but not the asset catalog for some reason.

由于某种原因,您的目标可能无法找到捆绑包而非资产目录。