在制作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"
- #define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME] #define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
- NSString * getMyBundlePath( NSString * filename);
[/pre]编辑Utils.m:
[pre]
- #import "Utils.h"
- NSString* getMyBundlePath( NSString * filename) {
- NSBundle * libBundle = MYBUNDLE ; if ( libBundle && filename ){
- NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename]; NSLog ( @"%@" ,s);
- return s; }
- return nil ; }
[/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
**这种做法缺点:通用静态库太大