Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image from CocoaPods resource bundle

Tags:

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 the library, I've included a line like this:

s.resource_bundles = {'IXMapKitResources' => ['IXMapKit/Resources/*']}

One of the resources in the bundle is an asset catalog with multiple image sets, one of which is called 'IXMKAnnotationIcons-Normal-Accident'. The following code returns a nil:

UIImage * image = [UIImage imageNamed: @"IXMKAnnotationIcons-Normal-Accident"];

I found an article on mokacoding.com describing how to load fonts from a pod, but this didn't work for me:

- (UIImage *) ixmk_imageNamed: (NSString *) name
{
    NSString * IXMKResourceBundleName = @"IXMapKitResources.bundle";
    NSString * resourceName = [NSString stringWithFormat: @"%@/%@", IXMKResourceBundleName, name];

    NSString * imageTypeString = [self ixmk_stringForImageType: imageType];
    NSURL * url = [[NSBundle mainBundle] URLForResource: resourceName withExtension: imageTypeString];

    NSData * imageData = [NSData dataWithContentsOfURL: url];
    if (imageData != nil)
    {
        CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef) imageData);
        CGImageRef imageRef = [self ixmk_imageFromDataProvider: provider imageType: imageType];

        UIImage * image = [UIImage imageWithCGImage: imageRef];

        CFRelease(imageRef);
        CFRelease(provider);

        return image;
    }
    else
    {
        return nil;
    }
}

The CocoaPods webpage describing the resources keyword has the following warning:

We strongly recommend library developers to adopt resource bundles as there can be name collisions using the resources attribute. Moreover resources specified with this attribute are copied directly to the client target and therefore they are not optimized by Xcode.

I'm at a loss of what to do here.

like image 467
David Potter Avatar asked May 23 '14 17:05

David Potter


1 Answers

This turns out to be a problem with asset catalogs, not CocoaPods or bundles. Moving the images out of the asset catalog solved the problem. It looks like Apple doesn't support asset catalogs in secondary resource bundles. Pity.

like image 50
David Potter Avatar answered Oct 14 '22 21:10

David Potter