iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework

转载自:http://blog.****.net/mylizh/article/details/38734005

一、首先将资源文件打包成bundle


    由于bundle是静态的,所以可以将“iOS开发之静态库(三)—— 图片、界面xib等资源文件封装到.a静态库”中生成的“MyToolsWithAssetsA.bundle”文件直接拿过来使用。


二、创建静态框架


创建过程参考“iOS开发之静态库(四)—— 静态框架framework制作”,里面介绍非常详细。


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework



静态库代码借用iOS开发之静态库(三)—— 图片、界面xib等资源文件封装到.a静态库”中文件


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


代码就不做任何修改了,直接使用


[objc] view plain copy
  1. //  
  2. //  BundleTools.h  
  3. //  MyToolsWithAssetsA  
  4. //  
  5. //  Created by LZH on 14-8-15.  
  6. //  Copyright (c) 2014年 LZH. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. #define BUNDLE_NAME @"MyToolsWithAssetsA"  
  12.   
  13. @interface BundleTools : NSObject  
  14.   
  15. + (NSString *)getBundlePath: (NSString *) assetName;  
  16. + (NSBundle *)getBundle;  
  17.   
  18. @end  

[objc] view plain copy
  1. //  
  2. //  BundleTools.m  
  3. //  MyToolsWithAssetsA  
  4. //  
  5. //  Created by LZH on 14-8-15.  
  6. //  Copyright (c) 2014年 LZH. All rights reserved.  
  7. //  
  8.   
  9. #import "BundleTools.h"  
  10.   
  11. @implementation BundleTools  
  12.   
  13. + (NSBundle *)getBundle{  
  14.       
  15.     return [NSBundle bundleWithPath: [[NSBundle mainBundle] pathForResource: BUNDLE_NAME ofType@"bundle"]];  
  16. }  
  17.   
  18. + (NSString *)getBundlePath: (NSString *) assetName{  
  19.       
  20.     NSBundle *myBundle = [BundleTools getBundle];  
  21.       
  22.     if (myBundle && assetName) {  
  23.           
  24.         return [[myBundle resourcePath] stringByAppendingPathComponent: assetName];  
  25.     }  
  26.       
  27.     return nil;  
  28. }  
  29.   
  30.   
  31. @end  

[objc] view plain copy
  1. //  
  2. //  ViewController1.h  
  3. //  MyToolsWithAssetsADemo  
  4. //  
  5. //  Created by LZH on 14-8-15.  
  6. //  Copyright (c) 2014年 LZH. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface ViewController1 : UIViewController  
  12.   
  13. @property (strongnonatomicUIImageView *imageView;  
  14.   
  15. @end  

[objc] view plain copy
  1. //  
  2. //  ViewController1.m  
  3. //  MyToolsWithAssetsADemo  
  4. //  
  5. //  Created by LZH on 14-8-15.  
  6. //  Copyright (c) 2014年 LZH. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController1.h"  
  10. #import "BundleTools.h"  
  11.   
  12. #import <QuartzCore/QuartzCore.h>  
  13.   
  14. @interface ViewController1 ()  
  15.   
  16. @end  
  17.   
  18. @implementation ViewController1  
  19. @synthesize imageView = _imageView;  
  20.   
  21. - (id)init{  
  22.       
  23.     NSBundle *myBundle = [BundleTools getBundle];  
  24.       
  25.     //self = [super initWithNibName: @"ViewController1" bundle: nil];  
  26.     //从bundle中获取界面文件  
  27.     self = [super initWithNibName: [NSString stringWithUTF8String: object_getClassName(self)] bundle: myBundle];  
  28.     if (self) {  
  29.         // Custom initialization  
  30.     }  
  31.     return self;  
  32. }  
  33.   
  34. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  35. {  
  36.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  37.     if (self) {  
  38.         // Custom initialization  
  39.     }  
  40.     return self;  
  41. }  
  42.   
  43. - (void)viewDidLoad  
  44. {  
  45.     [super viewDidLoad];  
  46.     // Do any additional setup after loading the view from its nib.  
  47.       
  48.     _imageView = [[UIImageView alloc] initWithFrame: CGRectMake(50100220220)];  
  49.       
  50.     //_imageView.image = [UIImage imageNamed: @"0001.jpg"];  
  51.     //从bundle中获取图片资源  
  52.     _imageView.image = [UIImage imageWithContentsOfFile: [BundleTools getBundlePath@"0001.jpg"]];  
  53.       
  54.     //给图片增加圆角  
  55.     _imageView.layer.cornerRadius = 20;  
  56.     _imageView.layer.masksToBounds = YES;  
  57.     _imageView.layer.borderWidth = 3;  
  58.     _imageView.layer.borderColor = [UIColor orangeColor].CGColor;  
  59.       
  60.     [self.view addSubview: _imageView];  
  61.       
  62. }  
  63. - (IBAction)backButton:(id)sender {  
  64.     [self dismissViewControllerAnimatedYES completion: NULL];  
  65. }  
  66.   
  67. @end  

加入源文件和头文件


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


修改配置选项


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


快捷键"Command + B"编译,分别生成真机版本和模拟器版本的framework


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


合并生成综合版本的framework


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


准备工作完成!


三、创建测试工程


创建单视图模板


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


工程命名


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


导入bundle资源包,此处的bundle就不修改名字了,修改了之后使用到它的地方都要修改,偷下懒...


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


导入framework静态框架


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


编写测试代码


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


[objc] view plain copy
  1. //  
  2. //  ViewController.m  
  3. //  MyToolsWithAssetsFrameworkTest  
  4. //  
  5. //  Created by LZH on 14-8-21.  
  6. //  Copyright (c) 2014年 LZH. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10. #import <MyToolsWithAssets/ViewController1.h>  
  11.   
  12. @interface ViewController ()  
  13.   
  14. @end  
  15.   
  16. @implementation ViewController  
  17.   
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view, typically from a nib.  
  22. }  
  23.   
  24. - (void)didReceiveMemoryWarning  
  25. {  
  26.     [super didReceiveMemoryWarning];  
  27.     // Dispose of any resources that can be recreated.  
  28. }  
  29.   
  30. //按钮响应函数  
  31. - (IBAction)buttonPressed:(id)sender {  
  32.       
  33.     ViewController1 *view1 = [[ViewController1 alloc] init];  
  34.     view1.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;   //界面切换动画效果  
  35.     [self presentViewController: view1 animatedYES completion: NULL];  
  36. }  
  37.   
  38.   
  39. @end  

快捷键"Command + R"运行

真机版本运行结果自己去试吧

模拟器版本运行结果


iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework


-- Over --