无法从 CocoaPods 中的 .xcasset 加载图像 [英] Can't load images from .xcasset in CocoaPods

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

问题描述

我正在创建一个 Pod,并且我有一个想要使用的图像资产目录.在我的 .podspec 文件中,我的设置如下:

I'm creating a pod and I have an image asset catalog I'd like to use. In my .podspec file, I have it set up like this:

s.resource_bundles = {
  'MyPodResources' => ['*.xcassets']
}

Images.xcassets 位于 pod 的根目录中.

and the Images.xcassets is in the root directory of the pod.

当我尝试使用 imageNamed() 加载图像时,它不起作用.我没有收到错误或警告,但没有显示任何图像.

When I'm try to load images using imageNamed(), it's just not working. I don't get an error or a warning but no images are displayed.

这是有趣的部分 - 如果我尝试在示例应用程序的 Main.storyboard 中添加图像,我可以选择该图像并且它在 Interface Builder 中显示良好.但是,当我运行示例应用程序时,图像不可见.

Here's the fun part - if I try to add an image in my Main.storyboard in the example application, I can select the image and it's showing up fine in the Interface Builder. However, when I run the example app, the image is not visible.

我已经查看了 GH 上的所有问题,但仍然找不到解决方案......这是 Xcode 7/iOS 9 问题吗?

I've looked through all issues on GH and still can't find a solution to this ... Is it an Xcode 7/iOS 9 issue?

谢谢!

推荐答案

最后写了一个 UIImage 的扩展,放到 pod 目录下.它看起来像这样:

In the end, I wrote an extension for UIImage and put it in the pod directory. It looks like this:

class func bundledImage(named: String) -> UIImage? {
    let image = UIImage(named: named)
    if image == nil {
        return UIImage(named: named, inBundle: NSBundle(forClass: MyBasePodClass.classForCoder()), compatibleWithTraitCollection: nil)
    } // Replace MyBasePodClass with yours
    return image
}

我正在使用它:imageView.image = UIImage.bundledImage("icon-grid")

就是这样,希望有人觉得这有用!

That's it, hope someone finds this useful!

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

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