ios - 无法在 CocoaPods 中从 .xcasset 加载图像

标签 ios xcode swift cocoapods

我正在创建一个广告连播并且我有一个我想使用的图像 Assets 目录。 在我的 .podspec 文件中,我将其设置如下:

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

并且 Images.xcassets 位于 pod 的根目录中。

当我尝试使用 imageNamed() 加载图像时,它就是不工作。我没有收到错误或警告,但没有显示图像。

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

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

谢谢!

最佳答案

最后我写了一个UIImage的扩展放在pod目录下。它看起来像这样:

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")

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

关于ios - 无法在 CocoaPods 中从 .xcasset 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063233/

相关文章:

ios - 使用编码器 aDecoder : NSCoder 推送 ViewController

ios - fetchBatchSize 不适用于 NSFetchRequest

ios - Firebase 事件监听器从未触发(iOS 应用程序)

ios - 'NSMutableRLEArray objectAtIndex:effectiveRange::Out of bounds'

xcode - presentViewController 错误 - AppDelegate 没有可见的@interface

ios - Command/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang 退出失败

ios - 如何在swiftui列表中获取textField值

javascript - 如何让使用 JavaScript 制作的应用程序为其他 Apple 设备自动调整大小?

ios - ScrollView.contentSize 不适用于 IOS 7

ios - swift 扩展