Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从NSBundle中的Assets.car(xcassets的编译版本)加载映像?_Ios_Cocoapods_Xcasset - Fatal编程技术网

Ios 如何从NSBundle中的Assets.car(xcassets的编译版本)加载映像?

Ios 如何从NSBundle中的Assets.car(xcassets的编译版本)加载映像?,ios,cocoapods,xcasset,Ios,Cocoapods,Xcasset,简而言之: 如何从NSBundle中编译的Assets.car加载图像 完整版本: 我正在将一套应用程序转换为使用CocoaPods。每个应用程序都依赖一个名为Core的共享pod Core包括代码文件、xib文件和几个xcaset文件 以下是Podspec中创建资源包的Core的相关行: s.resource_bundles = {'CoreResources' => ['Core/Resources/*']} Podspec通过Podspec lint,并正确构建依赖它的主项目

简而言之:

如何从
NSBundle
中编译的
Assets.car
加载图像

完整版本:

我正在将一套应用程序转换为使用
CocoaPods
。每个应用程序都依赖一个名为
Core
的共享pod

Core
包括代码文件、
xib
文件和几个
xcaset
文件

以下是
Podspec
中创建资源包的
Core
的相关行:

  s.resource_bundles = {'CoreResources' => ['Core/Resources/*']}
Podspec
通过
Podspec lint
,并正确构建依赖它的主项目

但是,
Core
中的任何
xasset
文件中的图像均未显示

我(天真地)尝试使用
UIImage
上的类别加载图像,如下所示:

@implementation UIImage (Bundle)

+ (UIImage *)imageNamed:(NSString *)name bundle:(NSBundle *)bundle
{
    if (!bundle)
        return [UIImage imageNamed:name];

    UIImage *image = [UIImage imageNamed:[self imageName:name forBundle:bundle]];
    return image;
}

+ (NSString *)imageName:(NSString *)name forBundle:(NSBundle *)bundle
{
    NSString *bundleName = [[bundle bundlePath] lastPathComponent];
    name = [bundleName stringByAppendingPathComponent:name];
    return name;
}
@end
以前,
核心
是一个子模块,该解决方案运行良好。然而,在检查我以前的
文件(与
包分开)时,我注意到所有图像都只是被复制到
。。。i、 e

Image.png
Image@2x.png
等都在捆绑包中

检查
CocoaPods
生成的捆绑包后,它包含一个

Assets.car

据我所知,它是上述
核心
子目录中所有
xcaste
文件的组合编译版本

如何从这个
Core
资源包中编译的
Assets.car
加载图像

作为一名黑客,我想我可以……

以下是一个例子:

spec.resource = "Resources/HockeySDK.bundle"
这似乎表明可以在Xcode中手动创建捆绑包,并让CoCoapod简单地复制它

不过,这与其说是一个解决方案,不如说是一个黑客行为


我相信CocoaPods(V0.29+)可以完全处理这个问题…?

我和你的情况一样,最后我使用了你提到的“黑客”,但它在pod安装过程中是自动的,所以更易于维护

在我的播客里我有

# Pre-build resource bundle so it can be copied later
  s.pre_install do |pod, target_definition|
    Dir.chdir(pod.root) do
      command = "xcodebuild -project MyProject.xcodeproj -target MyProjectBundle CONFIGURATION_BUILD_DIR=Resources 2>&1 > /dev/null"
      unless system(command)
        raise ::Pod::Informative, "Failed to generate MyProject resources bundle"
      end
    end
  end
然后在稍后的podspec中:

  s.resource = 'Resources/MyProjectBundle.bundle'
这里的技巧是在安装pod之前构建bundle,这样就可以使用.bundle,然后就可以像一直在源代码中一样链接它。这样我就可以轻松地在bundle目标中添加新的资源/图像/xib,并对它们进行编译和链接。工作起来很有魅力

我在NSBundle+MyResources上有一个类别,允许轻松访问捆绑资源:

+ (NSBundle *)myProjectResources
{
    static dispatch_once_t onceToken;
    static NSBundle *bundle = nil;
    dispatch_once(&onceToken, ^{
        // This bundle name must be the same as the product name for the resources bundle target
        NSURL *url = [[NSBundle bundleForClass:[SomeClassInMyProject class]] URLForResource:@"MyProject" withExtension:@"bundle"];
        if (!url) {
            url = [[NSBundle mainBundle] URLForResource:@"MyProject" withExtension:@"bundle"];
        }
        bundle = [NSBundle bundleWithURL:url];
    });
    return bundle;
}
因此,如果您想加载(例如)核心数据模型:

NSURL *modelURL = [[NSBundle myProjectResources] URLForResource:@"MyModel" withExtension:@"momd"];
我还提供了一些方便的方法来访问图像:

+ (UIImage *)bundleImageNamed:(NSString *)name
{
    UIImage *imageFromMainBundle = [UIImage imageNamed:name];
    if (imageFromMainBundle) {
        return imageFromMainBundle;
    }

    NSString *imageName = [NSString stringWithFormat:@"MyProject.bundle/%@", name];
    UIImage *imageFromBundle = [UIImage imageNamed:imageName];
    if (!imageFromBundle) {
        NSLog(@"Image not found: %@", name);
    }
    return imageFromBundle;
}

我还没有失败过。

+1:是的,我也做了类似的事情。实际上,您可以让CoCoapod完全为您创建资源包,而不需要预安装挂钩。我会尽快把它写下来,作为解决这个问题的另一个可能的方法。@JRG开发者真棒,很想看到它。很想看到现代的非黑客软件version@FullDecent我的最新版本不再使用预安装步骤,并且在s.resource\u bundle配置中有一个小的更改。其余的都一样。