情节提要,UIImageView.从包中加载图像 [英] Storyboard, UIImageView. Load image from bundle

查看:125
本文介绍了情节提要,UIImageView.从包中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是: 我有几个共享xcasset的项目/目标. 我想要做的是在没有代码的情况下在情节提要中设置图像.

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.

我创建了UIImageView扩展,但是没有想到最好的解决方案.

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?

谢谢.

推荐答案

因此,基于对

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:

  1. 创建一个框架项目来容纳您的共享图像(也非常适合共享代码!):文件>新建>项目> iOS> Cocoa Touch Framework .
  2. 在框架文件>新建>文件> iOS>资源>资产目录中创建一个新的资产目录.
  3. 将您的图像导入资产目录.
  4. 创建新的工作区:文件>新建>工作区.
  5. 将您的应用程序和框架都添加到工作区:文件>将文件添加到WorkspaceName .
  6. 将框架作为嵌入式二进制文件"包含在应用程序的项目中:单击项目导航器中的蓝色项目文件图标,转到常规项目设置并找到嵌入式二进制文件强>.点击 + ,然后从列表中选择框架(例如ExampleFramework.framework).
  1. Create a framework project to house your shared images (it's great for shared code too!): File > New > Project > iOS > Cocoa Touch Framework.
  2. Create a new Asset Catalogue within the framework File > New > File > iOS > Resource > Asset Catalogue.
  3. Import your images into the Asset Catalogue.
  4. Create a new workspace: File > New > Workspace.
  5. Add both your app and the framework to the workspace: File > Add Files to WorkspaceName.
  6. 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 { ... }

...但是,要在 storyboard或XIB 中使用它,还需要一步.现在可以从Interface Builder中选择图像名称(甚至在选择时显示在其中),情节提要只能引用它们自己的包,因此在应用程序运行时会出现以下错误,并且图像将丢失:

...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:

无法加载从笔尖引用的"ImageName"图像 标识符为"BundleName"的捆绑软件

Could not load the "ImageName" image referenced from a nib in the bundle with identifier "BundleName"

要解决此问题,您需要转到工作区的导航器(左侧面板),然后将资产目录(即.xcassets文件)从框架拖到应用程序项目本身内的某个位置(例如项目的根文件夹). 在显示的对话框中,确保在添加到目标列表中选中了您的应用,并且确保未选中以下选项未选中.

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.

完成此操作后,就可以像使用故事板中的任何其他内容一样使用该目录中的图像了. XIB.

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.从包中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆