从Objective-c中的.bundle加载png时出错 [英] Error loading pngs from a .bundle in Objective-c

查看:261
本文介绍了从Objective-c中的.bundle加载png时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Xcode项目中构建一个框架.为了构建框架,我创建了一个聚合目标.该框架由静态库,头文件和.png资产捆绑包MyFrameworkResources.bundle组成.构建聚合时,所有内容均已成功创建,并且捆绑包存在于我的产品"目录中.

I'm building a framework in my Xcode project. To build the framework, I created an aggregate target. The framework consists of the static library, header files, and bundle of .png assets, MyFrameworkResources.bundle. When I build the aggregate, everything is successfully created and the bundle exists in my Products directory.

在我的Xcode项目中,也是一个目标应用程序.该应用程序用于测试框架产品(静态库,头文件和捆绑软件).我已将静态库和MyFrameworkResources.bundle添加到目标应用程序的目标依赖项.我还添加了MyFrameworkResources.bundle在目标应用程序的构建阶段中复制捆绑资源.

Also in my Xcode project is a target Application. This application is for testing the framework products (static library, header files, and bundle). I've added the static library and MyFrameworkResources.bundle to the Target Dependencies of the target Application. I've also added MyFrameworkResources.bundle to Copy Bundle Resources in Build Phases of the target Application.

我可以很好地使用静态库中的所有文件,但是在尝试从包中加载资源时出现错误.这就是我从捆绑中加载资产的方式:

I can use all files from the static library just fine, but I get an error when trying to load resources from the bundle. This is how I'm loading assets from the bundle:

NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString *frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"MyFrameworkResources.bundle"];
NSBundle *frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath];
NSLog(@"bundle: %@", frameworkBundle);

NSError *error;
[frameworkBundle loadAndReturnError:&error]; // <-- THIS RETURNS AN ERROR
NSLog(@"error: %@", error);
UIImage *image = [UIImage imageWithContentsOfFile:[frameworkBundle pathForResource:@"AnyImage" ofType:@"tiff"]]; // <-- EDIT, USE "tiff", NOT "png"

当我记录该捆绑包时,我得到了:

When I log the bundle, I get this:

套装: NSBundle/Users/my_comp/Library/Application Support/iPhone Simulator/6.1/Applications/blah-blah-blah/MyApp.app/MyFrameworkResources.bundle(尚未加载)

当我记录错误时,我得到了:

When I log the error, I get this:

错误: Error Domain = NSCocoaErrorDomain代码= 4由于无法找到其可执行文件,因此无法加载"MyFrameworkResources"捆绑软件." UserInfo = 0x7535ee0 {NSLocalizedRecoverySuggestion =尝试重新安装捆绑软件.,NSLocalizedFailureReason =无法找到捆绑软件的可执行文件.,NSLocalizedDescription =无法加载捆绑软件"MyFrameworkResources",因为找不到可执行文件.,NSBundlePath =/Users /my_comp/Library/Application Support/iPhone Simulator/6.1/Applications/blah-blah-blah/MyApp.app/MyFrameworkResources.bundle}

因此要仔细检查,我在计算机上进入了/Users/my_comp/Library/Application Support/iPhone Simulator/6.1/Applications/blah-blah-blah/MyApp.app/,而MyFrameworkResources.bundle实际上在那里.所以现在我很茫然.我曾尝试清理汇总目标,清理目标应用程序,删除目标应用程序,但是没有运气.

So to double check, I cd'd into /Users/my_comp/Library/Application Support/iPhone Simulator/6.1/Applications/blah-blah-blah/MyApp.app/ on my computer, and the MyFrameworkResources.bundle was actually there. So now I'm at a loss. I've tried cleaning the aggregate target, cleaning the target Application, deleting the target Application, and no luck.

知道为什么我不能从捆绑软件中加载图像吗?(抱歉,冗长的说明)

Any idea why I can't load images from the bundle? (sorry for the lengthy description)

Fyi,我一直在使用这个很棒的教程作为构建框架和捆绑软件的准则.

Fyi, I've been using this great tutorial as a guideline to build the framework and bundle.

推荐答案

在对此进行了更多努力之后,我决定将放入MyFrameworkResources.bundle中,只是要仔细检查所有资产是否都包括在内.事实证明,Xcode将.png文件重命名为.tiff文件,并删除了视网膜资产.经过进一步调查,我发现 Xcode将视网膜和非视网膜.png文件自动组合到一个多页TIFF"文件中.这是参考.

Ah after struggling on this a bit more, I cd'd into MyFrameworkResources.bundle just to double check that all the assets were included. It turns out that Xcode renamed the .png files to .tiff files, and removed the retina assets. After further investigation, I discovered that Xcode automatically combines the retina and non-retina .png files into a "one multi-page TIFF". Here's a reference.

解决方案,使用ofType:@"tiff"代替ofType:@"png":

UIImage *image = [UIImage imageWithContentsOfFile:[frameworkBundle pathForResource:@"AnyImage" ofType:@"tiff"]];

这篇关于从Objective-c中的.bundle加载png时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆