How to use images asset catalog in cocoapod library for iOS

前端 未结 11 2368
粉色の甜心
粉色の甜心 2020-12-15 16:38

I have a cocoapod library which contains assets in 2 formats:

  • a .storyboard
  • XCode asset catalog .xcassets (with images)

my podsp

11条回答
  •  暖寄归人
    2020-12-15 17:19

    Well, image asset catalogs are not supported via pods - just include a resource bundle that contains your image files in your podspec like so:

    s.subspec 'Resources' do |resources|
        resources.resource_bundle = {'MyBundle' => ['Resources/**/*.{png}']}
    
    end
    

    and access the images safely from the pod like that:

    +(NSBundle *)getResourcesBundle
    {
        NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle bundleForClass:[self class]] URLForResource:@"MyBundle" withExtension:@"bundle"]];
        return bundle;
    }
    
    
    +(UIImage *)loadImageFromResourceBundle:(NSString *)imageName
    {
        NSBundle *bundle = [MyClass getResourcesBundle];
        NSString *imageFileName = [NSString stringWithFormat:@"%@.png",imageName];
        UIImage *image = [UIImage imageNamed:imageFileName inBundle:bundle compatibleWithTraitCollection:nil];
        return image;
    }
    

    That solves all issues of handling images/resources within a cocoapod.

提交回复
热议问题