Loading resource_bundle error in cocoapods

断了今生、忘了曾经 提交于 2019-12-25 01:28:14

问题


I am trying to make my own cocoapods, I used images. When I follow the tutorial, I get an error.

.podspec

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

Class/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;
}

error

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

Screenshot

What is wrong with my code? thks


回答1:


Nothing wrong with your code.

Cocoapods on version 1.0.1 supports Assets Catalogs. Please check if yours isn't below it running pod --version in Terminal.

If a new pod install does not solved, there is an issue with the xCode New Build System on Assets Catalogs (as mentioned Here) that could be your problem.

You can change the Build System on File > Workspace Settings... > Build System. Try setting it to 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;
}


来源:https://stackoverflow.com/questions/57833676/loading-resource-bundle-error-in-cocoapods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!