`
zxs19861202
  • 浏览: 908863 次
  • 性别: Icon_minigender_1
  • 来自: 湖北—》上海
社区版块
存档分类
最新评论

iphone 静态库读取资源文件

    博客分类:
  • ios
阅读更多

在制作iphone静态库中并不能包含资源文件,虽然我们将资源文件(.png文件)拷贝到静态库工程中,但实际上这些.png是不会添加到target的,也就是说编译结果中并不包含这些资源,因此如果此时调用静态库,所有的资源(字符串、图片)都是缺失的。
我们可以把资源建立成单独的束(Bundle)。
新建工程“ Mac OS X -> Framework & Library -> Bundle ”,命名为:yhyLibraryBundle。
然后把上面.png文件拷进Resouces中去。编译,生成yhyLibraryBundle.bundle文件。
返回静态库工程,新建一个类:Utils 。
编辑Utils.h:
[pre]

    #define MYBUNDLE_NAME @ "yhyLibraryBundle.bundle"  
  1. #define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]  
  2. #define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]  
  3. NSString * getMyBundlePath( NSString * filename);  

[/pre]编辑Utils.m:
[pre]

    #import "Utils.h"  
  1. NSString* getMyBundlePath( NSString * filename)  
  2. {  
  3. NSBundle * libBundle = MYBUNDLE ;  
  4. if ( libBundle && filename ){  
  5. NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename];  
  6. NSLog ( @"%@" ,s);  
  7. return s;  
  8. }  
  9. return nil ;  
  10. }  

[/pre]函数getMyBundlePath可以取得束yhyLibraryBundle中具体资源的绝对文件路径,如:
[pre]

    /Users/kmyhy/Library/Application Support/iPhone Simulator/4.2/Applications/8213652F-A47E-456A-A7BB-4CD40892B66D/yhyLibTest.app/    yhyLibraryBundle.bundle/Contents/Resources/radio.png  

[/pre]同时,修改CheckButton.m中的代码,导入Utils.h头文件,把其中获取图片的代码由imageNamed修改为imageWithContentsOfFile,如:
[pre]

    [ icon setImage :[ UIImage imageWithContentsOfFile : getMyBundlePath ( checkname )]];  

[/pre]即通过绝对路径读取图片资源。

 

在运行生成.a文件之后不能通用模拟器和真机,通用的做法为:

 

可以使用命令行工具lipo将适用于真机与模拟器的静态库合二为一,操作如下

user#lipo -create /ospath/libname.a /simulatorpath/libname.a -output /allInOnelibName.a 即可

用如下命令可以看到合并后静态库支持的cpu架构信息

user#lipo -info /allInOnelibName.a

Architectures in the fat file: /Users/ipi/Desktop/libDemoLib_1.a are: armv7 i386

 

**这种做法缺点:通用静态库太大

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics