iOS Bundle文件使用

2017-03-18  本文已影响698人  hello老文

温故而知新, 记录下

Settings Bundle

  1. 在Xcode上command+n, 选择创建Settings Bundle, 名字Test.bundle;

  2. Test.bundle上右键New Folder新建文件夹images, 把一个图片文件laowen.png拖进去;

  3. 把bundle内laowen.png图片拿出使用:

NSString *strResourcesBundle = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"bundle"];
NSString *strC = [[NSBundle bundleWithPath:strResourcesBundle] pathForResource:@"laowen" ofType:@"png" inDirectory:@"images"];
UIImage *imgC = [UIImage imageWithContentsOfFile:strC];
self.iv.image = imgC;
```

macOS下的Bundle

  1. 在项目的TARGETS下点+号添加一个bundle, 选macOS下的Bundle创建一个MyBundle;

  2. MyBundleBuild SettingBase SDK改成Latest iOS;

  3. MyBundle内新建images文件夹并添加图片laowen.png;

  4. images拖入项目,要选Create groups;

  5. 选择MyBundle项目, Command+B编译;

  6. 在项目的Build Phases添加一个Run Script, 加入下面脚本:

    cp -R ${BUILT_PRODUCTS_DIR}/MyBundle.bundle ${BUILT_PRODUCTS_DIR}/${TARGET_NAME}.app
    
  7. 在项目中使用这个Bundle内的资源:

    #define MyLibBUNDLE_NAME @ "MyBundle.bundle"
    #define MyLibBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:MyLibBUNDLE_NAME]
    #define MyLibBUNDLE [NSBundle bundleWithPath: MyLibBUNDLE_PATH]
    
    UIImage *img = [UIImage imageWithContentsOfFile:[[MyLibBUNDLE resourcePath] stringByAppendingPathComponent:@"test.png"]];
    

其实, 直接建一个Bundle项目打包一个.bundle文件出来, 复制到需要的项目中使用, 这样比建一个Bundle附加在项目中这种做法更方便.

Mac检测图片格式的方法

Mac检测图片格式并不是使用后缀名,而是使用文件头信息来判断识别.可以通过UltraEdit来打开图片,16进制查看。

上一篇 下一篇

猜你喜欢

热点阅读