我的Xcode项目中还有一个目标应用程序.此应用程序用于测试框架产品(静态库,头文件和包).我已将静态库和MyFrameworkResources.bundle添加到目标应用程序的目标依赖项中.我还在目标应用程序的构建阶段中将MyFrameworkResources.bundle添加到Copy Bundle Resources.
我可以使用静态库中的所有文件,但是在尝试从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"
当我记录捆绑包时,我得到了这个:
束:
NSBundle / Users / my_comp / Library / Application Support / iPhone Simulator / 6.1 / Applications / blah-blah-blah / MyApp.app / MyFrameworkResources.bundle(尚未加载)
当我记录错误时,我得到了这个:
错误:
Error Domain = NSCocoaErrorDomain Code = 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实际上就在那里.所以现在我不知所措.我已经尝试清理聚合目标,清理目标应用程序,删除目标应用程序,没有运气.
知道为什么我无法从捆绑中加载图像吗? (对不起冗长的描述)
Fyi,我一直在使用this great tutorial作为构建框架和捆绑的指南.
解决方法
解决方案,使用Type:@“tiff”而不是ofType:@“png”:
UIImage *image = [UIImage imageWithContentsOfFile:[frameworkBundle pathForResource:@"AnyImage" ofType:@"tiff"]];