【问题标题】:Loading resource_bundle error in cocoapods在 cocoapods 中加载 resource_bundle 错误
【发布时间】:2019-09-07 12:26:34
【问题描述】:

我正在尝试制作自己的 cocoapods,我使用了图像。 当我按照教程进行操作时,出现错误。

.podspec

  s.resource_bundles = {
    'SSSlidingSelector' => ['SSSlidingSelector/Assets/*.xcassets']
  }

类/SlidingSelector.m

- (UIImage *)getImageWithName:(NSString *)imageName {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *url = [bundle URLForResource:@"SSSlidingSelector" withExtension:@"bundle"];
    NSBundle *targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:imageName
                                inBundle:targetBundle
           compatibleWithTraitCollection:nil];
    return image;
}

错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSBundle initWithURL:]: nil URL argument'

截图

我的代码有什么问题? 谢谢

【问题讨论】:

  • 我刚刚对此进行了测试,该功能按预期工作。您使用的是哪个版本的 Cocoapods?
  • 1.7.5 @alxlives

标签: ios objective-c cocoapods nsbundle


【解决方案1】:

您的代码没有问题。

1.0.1 版的 Cocoapods 支持资产目录。请检查您的是否低于它在终端中运行pod --version

如果新的 pod install 未解决,则资产目录上的 xCode 新构建系统存在问题(如 Here 所述),这可能是您的问题。

您可以在File > Workspace Settings... > Build System 上更改构建系统。尝试将其设置为Legacy Build System

【讨论】:

    【解决方案2】:
    - (UIImage *)getImageWithName:(NSString *)imageName {
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        UIImage *image = [UIImage imageNamed:imageName
                                    inBundle:bundle
               compatibleWithTraitCollection:nil];
        return image;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-06
      • 2021-12-24
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      相关资源
      最近更新 更多