从iOS 8框架加载图像 [英] Load image from iOS 8 framework

查看:105
本文介绍了从iOS 8框架加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我正在编写的iOS 8框架中加载图像(在Swift中)。我正在使用Xcode 6 Beta 6

I'm trying to load an image from an iOS 8 framework that I'm writing (in Swift). I'm using Xcode 6 Beta 6

如果图像存储在我的框架 Images.xcassets中,则此代码不起作用(即加载图像)

This code does not work (i.e. load image) if the image is stored in my framework's Images.xcassets:

let image = UIImage(named: "Background.png")

如果图像存储在主机的 Images.xcassets 中应用程序(使用框架),然后正确加载图像(从框架内的代码)。

If the image is stored in an Images.xcassets of a host application (that uses the framework), then the image is loaded properly (from code inside the framework).

我可以看到框架的图像.xcassets 包含在复制包资源阶段。

I can see that the framework's Images.xcassets is included in the Copy Bundle Resources phase.

我也是使用故事板文件作为框架中的资源;正确加载。

I'm also using a storyboard file as a resource in the framework; and this loads properly.

我尝试重命名框架的Images.xcassets以避免与主机应用程序发生某种命名冲突,但这不起作用或者。

I've tried renaming the Images.xcassets of the framework to avoid some kind of naming collision with the host application, but this doesn't work either.

推荐答案

虽然@Renatus答案完全有效且解决了核心问题(需要指定框架包),我想要发布我带的解决方案,因为它更直接:

While @Renatus answer is perfectly valid and addresses the core issue (bundle for framework needs to be specified), I wanted to post the solution I went with since it's slightly more direct:

Swift 3.0 / 4.0

let image = UIImage(named: "YourImage", in: Bundle(for: YOURFRAMEWORKCLASS.self), compatibleWith: nil)

或者,您可以将此模式用于非,又名非静态 ,函数:

Alternatively, you can use this pattern for non-class, aka non-"static", functions:

let image = UIImage(named: "YourImage", in: Bundle(for: type(of: self)), compatibleWith: nil)

class 函数:

let image = UIImage(named: "YourImage", in: Bundle(for: self), compatibleWith: nil)

这些替代品更适合切割和粘贴。

These alternatives are better for cutting and pasting.

这篇关于从iOS 8框架加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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