How to load resource in cocoapods resource_bundle

前端 未结 8 2039
说谎
说谎 2020-12-01 10:43

I have struggled a lot to how to load resource in cocoapods resource_bundle.

The following is what i put in the .podspecs file.

s.sourc         


        
8条回答
  •  失恋的感觉
    2020-12-01 11:01

    I don't think any of the other answers have described the relationship to the Podspec. The bundle URL uses the name of the pod and then .bundle to find the resource bundle. This approach works with asset catalogs for accessing pod images both inside and outside of the pod.

    Podspec

    Pod::Spec.new do |s|
      s.name             = 'PodName'
      s.resource_bundles = {
        'ResourceBundleName' => ['path/to/resources/*/**']
      }
    end
    

    Objective-C

    // grab bundle using `Class` in pod source (`self`, in this case)
    NSBundle *bundle = [NSBundle bundleForClass:self.classForCoder];
    NSURL *bundleURL = [[bundle resourceURL] URLByAppendingPathComponent:@"PodName.bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithURL:bundleURL];
    UIImage *podImage = [UIImage imageNamed:@"image_name" inBundle:resourceBundle compatibleWithTraitCollection:nil];
    

    Swift

    See this answer.

提交回复
热议问题