从CocoaPods资源包加载图像

问题描述:

我正在创建一个私有库,其中包含库中组件引用的资源。该库与使用CocoaPods的应用程序共享。在库的.podspec中,我包含了这样一行:

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/*']}

捆绑包中的一个资源是资产目录具有多个图像集,其中一个称为IXMKAnnotationIcons-Normal-Accident。以下代码返回nil:

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"];

我发现 mokacoding.com上的文章描述了如何从pod中加载字体,但是这对我不起作用:

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;
    }
}

描述资源关键字的CocoaPods网页有以下警告:


我们强烈建议库开发人员采用资源包作为
,可以使用resources属性进行名称冲突。此外,使用此属性指定的
资源将直接复制到
客户端目标,因此它们不会被Xcode优化。

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.

我无法在这里做什么。

这证明资产目录存在问题,而不是CocoaPods或捆绑。将图像移出资产目录可以解决问题。看起来Apple不支持辅助资源包中的资产目录。可惜。

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.