Load image from CocoaPods resource bundle

后端 未结 7 2276
独厮守ぢ
独厮守ぢ 2021-02-02 11:48

I am creating a private library that includes resources that are referenced by components in the library. The library is shared with apps using CocoaPods. In the .podspec for

7条回答
  •  忘掉有多难
    2021-02-02 12:33

    In your pod file: images are in .xcassets so it can be easily organized with 1x, 2x, 3x

    s.resources = 'PodName/Assets/**'
    

    Usage: Swift

    let bundle = Bundle(for: ClassThatWillUseTheImage.self)
    let image = UIImage(named: "close", in: bundle, with: .none)
    

    Usage: Objective-C

    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    UIImage *image = [UIImage imageNamed:@"close" inBundle:bundle withConfiguration:nil];
    

    Tested in Version 11.4 (11E146) iOS 13.0+

    Using #imageLiteral(resourceName: "close") does not work however, it shows the image being read but will throw and error.

提交回复
热议问题