- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
简而言之:
如何从 NSBundle
中已编译的 Assets.car
加载图像?
完整版:
我正在将一套应用程序转换为使用 CocoaPods
。每个应用程序都依赖于一个名为 Core
的共享 pod。
Core
包括代码文件、xib
文件和几个 xcasset
文件。
这是 Podspec
中用于创建资源包的 Core
的相关行:
s.resource_bundles = {'CoreResources' => ['Core/Resources/*']}
Podspec
通过了 pod spec lint
,依赖它的主项目正确构建。
但是,Core
中的任何 xcasset
文件中的图像均未显示。
我(天真地)尝试使用 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
以前,Core
是一个子模块
,这个解决方案运行良好。然而,在检查我之前的 bundle
文件(与 main
bundle 分开)时,我注意到所有图像都被简单地复制到 bundle
... 即
Image.png
、Image@2x.png
等都在包中。
在检查 CocoaPods
生成的包时,它包含一个
Assets.car
据我所知,它是上述 Core
子目录中所有 xcasset
文件的组合编译版本。
我如何从这个编译过的 Assets.car
加载图像到这个 Core
资源包中?
作为黑客,我想我可以...
spec.resource = "Resources/HockeySDK.bundle"
这似乎表明可以在 Xcode 中手动创建包并让 CocoaPods 简单地复制它。
不过,这与其说是解决方案,不如说是hack。
我相信 CocoaPods (v 0.29+) 可以完全处理这个......?
最佳答案
我和你遇到了同样的情况,我最终采用了你提到的“hack”,但它在 pod 安装期间是自动的,因此更易于维护。
在我的 podspec 中有
# 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 install 之前构建 bundle,这样 .bundle 就可用了,然后就可以链接了,就像你一直在源代码中一样。这样我就可以轻松地在 bundle 目标中添加新的资源/图像/xibs,它们将被编译和链接。就像一个魅力。
我在 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;
}
我还没有失败过。
关于ios - 如何在 NSBundle 中从 Assets.car(xcassets 的编译版本)加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21857986/
我在我的应用程序中使用rails 4 和 ruby 1.9.3 以及fancybox2-rails gem,但是 Assets 管道存在一个普遍问题。如果我运行 rake task 进行预编译,那
我把我所有的 Assets 都放在我的包里例如myBundle/Resources/public/css/ 然后像这样加载它们: {% block stylesheets %} {{ parent()
我正在创建RoR-6应用程序,并从此行的application.html.erb文件中引发以下错误: javascript_include_tag 'application', 'data-turbo
我正在对 Play Subproject 功能进行更多扩展测试,如下所述:http://www.playframework.com/documentation/2.0/SBTSubProjects .
我是 symfony 2 和 Assetic 的新手。我想在 CSS 中使用 assetic 和 Sass。我使用自定义字体。我在资源下的包中创建文件夹“assets/css”,里面有 _base.s
这是我的目录结构(这只是一个测试项目): stan@mypc:/generate_assets$ sudo tree -L 3 -p . ├── [drwxr-xr-x] assets │ └─
我使用 node.js connect/express。 有人知道支持 TypeScript 源代码即时编译和缩小的 Assets 管理器吗? 知道如何以编程方式调用编译器吗? 我一直在使用 conn
因此,我们在 Assets 管道摘要方面遇到了一个小问题,因为我们有许多 HTML 模板作为 Assets (对于 AngularJS),并且我们使用 asset_path 在 Javascript
我想从iPad的照片库中获取图像URL。 当我尝试从Image Piicker的信息中获取 UIImagePickerControllerReferenceURL 时 我将URL设为: assets
我正在使用带有Assetic的Symfony 2.1.10版,并且在上次 Composer 更新之后,当我尝试运行php app/console assetic:dump时出现以下错误 Dumping
我的 Assets 管道有问题,我已经有一个名为 Assets 的资源/ Controller 。所以我已将 assets.prefix 选项更改为“/externals”。 config.asset
更新到 Expo sdk 34 后,出现此错误:TypeError:Asset.fromModule 不是函数。 (在“Asset.fromModule(image)”中,“Asset.fromMod
将 Play 框架项目从 2.2.4 迁移到 2.3.8 后遇到问题: 这是运行命令“activatorcompile”的错误输出: [error] ...\workspace\testproject
我在将 Google Analytics 添加到我的 Rails 4 应用程序时遇到了一些困难(参见 this post)。 我通过在 /public/assets/google-analytics.
我正在使用 Symfony2 和 Assetic。最近我一直在做很多 CSS 工作,所以在某个时候我需要命令 $ php app/console assetic:dump --env=prod --n
我正在 Android 开发中迈出第一步,并遇到了 Assets 一词。 据我了解, Assets 只是一个未被解析或引用为资源的文件。 这个词有更准确的定义吗?我应该在哪些情况下在我的应用程序中使用
我添加了 danial-farid--angular-file-upload通过 Rails Assets 将库添加到我的 Rails 4 应用程序。主库加载正常,但 sprockets 找不到它包含
这是我的代码,尽管编码非常粗糙: public void loadStack(AssetManager manager, String path) { String[] lis
我正在使用 Assets 管理我的 Symfony 2 框架中的 CSS 文件。它在生产模式下运行良好。 我的问题是在 Debug模式下,assetic 不断将我的文件合并为一个输出文件。这使得跟踪特
我想以编程方式从重复的相册中删除 Assets (照片不是)。我可以使用照片框架删除相册 我想知道如何从相册中删除 Assets 而不从照片应用中完全删除它。我想在多个地方使用它,例如将 Assets
我是一名优秀的程序员,十分优秀!