Storyboard, UIImageView. Load image from bundle(故事板,UIImageView.从包中加载图像)
问题描述
我的问题是:我有几个共享 xcasset 的项目/目标.我想做的是在没有代码的情况下在情节提要中设置图像.
据我所知,无法在不同包的情节提要中设置图像.
我创建了 UIImageView 扩展,但不认为是最好的解决方案.
有没有一种方法可以使用来自不同捆绑包的故事板中的资产?
谢谢.
所以,基于
这将在您的应用程序的主包中创建一个对框架中资产目录的引用,但不会创建它的副本.
完成此操作后,您可以像使用故事板中的任何其他图像一样使用该目录中的图像和XIB.
老实说,我不能保证这是否是最佳实践,但这是我发现的唯一方法,我认为这是一种减少项目之间重复的明智方法.如果您有兴趣,我已经发布了一个问题 此处尝试确定这是否会导致构建时的资产重复.
My issue is: I have several projects/targets with shared xcassets. What I want to do is to set images inside storyboard without code.
As I was able to find, there are no way to set image in storyboard from different bundle.
I created UIImageView extension, but don't think about like the best solution.
Is there exist a way to use assets in storyboard from different bundle?
Thanks.
So, based on answers to this question, I do know of one solution. Sounds like you've already done some of this, but just in the interest of giving a complete answer:
- Create a framework project to house your shared images (it's great for shared code too!): File > New > Project > iOS > Cocoa Touch Framework.
- Create a new Asset Catalogue within the framework File > New > File > iOS > Resource > Asset Catalogue.
- Import your images into the Asset Catalogue.
- Create a new workspace: File > New > Workspace.
- Add both your app and the framework to the workspace: File > Add Files to WorkspaceName.
- Include the framework as an 'Embedded Binary' within the app's project: Click the blue project file icon in the project navigator, go to the General project settings and locate Embedded binaries. Click + and select the Framework from the list (e.g. ExampleFramework.framework).
The above should be enough to allow you to use images from the framework's Asset Catalogue when they are called using code...
let frameworkBundle = Bundle(for: FrameWorkClass.self)
guard let image = UIImage(named: "MyImage", in: frameworkBundle, compatibileWith: nil) else { ... }
...however, to use it in a storyboard or XIB, there's one more step needed. Whilst the image names will now be selectable from Interface Builder (and even display there when selected), storyboards can only reference their own bundles, so it'll give you the following error when the app runs, and the image will be missing:
Could not load the "ImageName" image referenced from a nib in the bundle with identifier "BundleName"
To fix this, you need to go to the Workspace's navigator (left panel) and drag the Asset Catalogue (i.e. the .xcassets
file) from the framework to somewhere within the app project itself (e.g. the project's root folder).
In the dialog that appears, be sure that your app is checked in the Add to targets list, and that you leave the following option unchecked.
This will create a reference in your app's main bundle to the Asset Catalogue in the framework, but without creating a duplicate of it.
Once you've done that, you can use the images from that catalogue just like any other within your storyboards & XIBs.
Honestly, I can't vouch for if this is a best-practice, but it's the only way I've found to achieve what I also feel to be a sensible means of minimizing duplication between projects. In case you're interested, I've posted a question here to try and determine if this causes any duplication of the assets on build.
这篇关于故事板,UIImageView.从包中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:故事板,UIImageView.从包中加载图像
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01