How to use images asset catalog in cocoapod library for iOS

前端 未结 11 2323
粉色の甜心
粉色の甜心 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.

    0 讨论(0)
  • 2020-12-15 17:19

    Only this works for me (Swift 3 & Swift 4)

    public struct ImagesHelper {
      private static var podsBundle: Bundle {
        let bundle = Bundle(for: YourClass.self)
        return Bundle(url: bundle.url(forResource: "YourClass",
                                      withExtension: "bundle")!)!
      }
    
      private static func imageFor(name imageName: String) -> UIImage {
        return UIImage.init(named: imageName, in: podsBundle, compatibleWith: nil)!
      }
    
      public static var myImage: UIImage {
        return imageFor(name: "imageName")
      }
    }
    

    Then use it as below:

    ImagesHelper.myImage
    

    As in the @Chris Prince's answer, don't forget to update your podspec file like:

    s.resource_bundles = {
      'YourClass' => ['YourPodName/*/Assets.xcassets']
    }
    
    0 讨论(0)
  • 2020-12-15 17:20

    I can't still get it to work, with none of the solutions above. I have an Assets.xcassets file in my Pod.

    And inside my Pod classes i would like to access images from the Assets

    Specified like this:

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

    With this helper:

    public struct ImagesHelper {
        private static var podsBundle: Bundle {
            let bundle = Bundle(for: SWDownloadPlayButton.self)
            return Bundle(url: bundle.url(forResource: "SWDownloadPlayButton",
                                          withExtension: "bundle")!)!
        }
    
        private static func imageFor(name imageName: String) -> UIImage {
            return UIImage.init(named: imageName, in: podsBundle, compatibleWith: nil)!
        }
    
        public static var download_icon: UIImage {
            return imageFor(name: "download_icon")
        }
    }
    

    But whatever i do inside my Pod classes (not in the example project)...like this

     var centerImage: UIImage = ImagesHelper.download_icon.withRenderingMode(.alwaysTemplate) {
            didSet {
                updateImage()
            }
        }
    

    I still get a nil on the centerImage

    0 讨论(0)
  • 2020-12-15 17:22

    At least as of version 1.0.1 of Cocoapods, image asset catalogs are supported.

    In my Swift 4.2 code, I used:

    public static var GoogleIcon: UIImage {
        let bundle = Bundle(for: self)
        log.msg("bundle: \(bundle)")
        return UIImage(named: "GoogleIcon", in: bundle, compatibleWith: nil)!
    }
    

    In my pod spec, I used:

    s.resources = "SMCoreLib/Assets/*.xcassets"
    

    When I build using the simulator, the .car file does show up in the Framework:

    cd /Users/<snip>/SMCoreLib.framework 
    ls
    Assets.car  Info.plist  SMCoreLib
    
    0 讨论(0)
  • 2020-12-15 17:24

    Swift 3 version

    private func getImageFromBundle(name: String) -> UIImage {
       let podBundle = Bundle(for: YourClass.self)
       if let url = podBundle.url(forResource: "YourClass", withExtension: "bundle") {
          let bundle = Bundle(url: url)
          return UIImage(named: name, in: bundle, compatibleWith: nil)!
       }
       return UIImage()
    }
    
    0 讨论(0)
  • 2020-12-15 17:24

    If you're using SwiftUI, using the Image.init(_ name: String, bundle: Bundle?) initializer won't work, you have to initialize it like this:

     func getImage(named name: String) -> UIImage {
        // Use a random singleton class in your project.
        // Emphasis on class, structs won't work.
        let bundle = Bundle(for: [random class].self)
    
        if let image = UIImage(named: name, in: bundle, compatibleWith: nil) {
            return image
        }
    
        return .init()
    }
    
    Image(uiImage: getImage(named: "image_name"))
    
    0 讨论(0)
提交回复
热议问题