首页 > 其他 > 详细

如何在Framework中读取bundle中的Res

时间:2017-03-01 15:45:01      阅读:394      评论:0      收藏:0      [点我收藏+]

前因:

  因为公司上架前后的原因,外围的平台层部分提前上线,而我做的功能部分需要晚一些上线,是单独的一个工程在其他仓库开发。

我的资源文件放在Bundle中。合到主工程中,资源文件不用改,直接拖进去。倒是代码部分因为重名较多,花了大半天时间来改名字。

  过一段时间,需要将我们的代码以Framework的形式,放入另一个项目的平台层中。我的做法是,将代码打包进入Framework,然后资源文件在Bundle中,两部分拖进平台层,资源文件调用等等也都不用改。

因为Android就只有一个aar文件导入平台层。所以对方提出希望我也只提供一个Framework 包。

所以现在就需要改资源文件调用方式了。

以前的做法很简单:

NS_INLINE UIImage * UIResourceBundleSubMove(NSString *strPath){
    return [UIImage imageNamed:[NSString stringWithFormat:@"XXXX.bundle/images/move/%@.png",strPath]];
}

 

现在就是

#define FrameworkPath  [[NSBundle mainBundle] pathForResource:@"VivenSDK" ofType:@"framework"]
#define FrameworkBundle  [NSBundle bundleWithPath:FrameworkPath]
#define VivienBundle [NSBundle bundleWithPath:[FrameworkBundle pathForResource:@"Vivien" ofType:@"bundle"]] #define UIResourceBundleSubMove(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"images/move/%@",imageName] inBundle:VivienBundle compatibleWithTraitCollection:nil]

 

然后再把Framework再拖一份到主工程的Copy Bundle Resources

 

如果报错,可以通过代码的方式查看报错:

    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"VivienSDK" ofType:@"framework"];
    NSLog(@"path = %@", path);
    
    NSBundle *myBundle = [NSBundle bundleWithPath:path];
    NSLog(@"myBunlde = %@", myBundle);
    
    NSBundle *vivienResBundle= [NSBundle bundleWithPath:[myBundle pathForResource:@"Vivien" ofType:@"bundle"]];
    
    NSLog(@"vivienResBundle = %@", vivienResBundle);
    
    UIImage *iconImage = [UIImage imageNamed:[NSString stringWithFormat:@"Images/%@",@"icon_friend"] vivienResBundle compatibleWithTraitCollection:nil];
    NSLog(@"iconImage = %@", iconImage);

 

如何在Framework中读取bundle中的Res

原文:http://www.cnblogs.com/developer-qin/p/6484527.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!