码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-09-14 21:55:37      阅读:5447      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   strong   

 

编译环境:Macbook Air + OS X 10.9.2 + XCode5.1 + iPhone5s(iOS7.0.3)

 

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

 

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

 

二、创建静态框架

 

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

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

 

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

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

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

 

//
//  BundleTools.h
//  MyToolsWithAssetsA
//
//  Created by LZH on 14-8-15.
//  Copyright (c) 2014年 LZH. All rights reserved.
//

#import <Foundation/Foundation.h>

#define BUNDLE_NAME @"MyToolsWithAssetsA"

@interface BundleTools : NSObject

+ (NSString *)getBundlePath: (NSString *) assetName;
+ (NSBundle *)getBundle;

@end
//
//  BundleTools.m
//  MyToolsWithAssetsA
//
//  Created by LZH on 14-8-15.
//  Copyright (c) 2014年 LZH. All rights reserved.
//

#import "BundleTools.h"

@implementation BundleTools

+ (NSBundle *)getBundle{
    
    return [NSBundle bundleWithPath: [[NSBundle mainBundle] pathForResource: BUNDLE_NAME ofType: @"bundle"]];
}

+ (NSString *)getBundlePath: (NSString *) assetName{
    
    NSBundle *myBundle = [BundleTools getBundle];
    
    if (myBundle && assetName) {
        
        return [[myBundle resourcePath] stringByAppendingPathComponent: assetName];
    }
    
    return nil;
}


@end
//
//  ViewController1.h
//  MyToolsWithAssetsADemo
//
//  Created by LZH on 14-8-15.
//  Copyright (c) 2014年 LZH. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController1 : UIViewController

@property (strong, nonatomic) UIImageView *imageView;

@end
//
//  ViewController1.m
//  MyToolsWithAssetsADemo
//
//  Created by LZH on 14-8-15.
//  Copyright (c) 2014年 LZH. All rights reserved.
//

#import "ViewController1.h"
#import "BundleTools.h"

#import <QuartzCore/QuartzCore.h>

@interface ViewController1 ()

@end

@implementation ViewController1
@synthesize imageView = _imageView;

- (id)init{
    
    NSBundle *myBundle = [BundleTools getBundle];
    
    //self = [super initWithNibName: @"ViewController1" bundle: nil];
    //从bundle中获取界面文件
    self = [super initWithNibName: [NSString stringWithUTF8String: object_getClassName(self)] bundle: myBundle];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _imageView = [[UIImageView alloc] initWithFrame: CGRectMake(50, 100, 220, 220)];
    
    //_imageView.image = [UIImage imageNamed: @"0001.jpg"];
    //从bundle中获取图片资源
    _imageView.image = [UIImage imageWithContentsOfFile: [BundleTools getBundlePath: @"0001.jpg"]];
    
    //给图片增加圆角
    _imageView.layer.cornerRadius = 20;
    _imageView.layer.masksToBounds = YES;
    _imageView.layer.borderWidth = 3;
    _imageView.layer.borderColor = [UIColor orangeColor].CGColor;
    
    [self.view addSubview: _imageView];
    
}
- (IBAction)backButton:(id)sender {
    [self dismissViewControllerAnimated: YES completion: NULL];
}

@end

 

 

加入源文件和头文件

 

bubuko.com,布布扣

 

修改配置选项

 

bubuko.com,布布扣

 

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

 

bubuko.com,布布扣

 

合并生成综合版本的framework

 

bubuko.com,布布扣

 

准备工作完成!

 

三、创建测试工程

 

创建单视图模板

 

bubuko.com,布布扣

 

工程命名

 

bubuko.com,布布扣

 

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

 

bubuko.com,布布扣

 

导入framework静态框架

 

bubuko.com,布布扣

 

编写测试代码

 

bubuko.com,布布扣

 

//
//  ViewController.m
//  MyToolsWithAssetsFrameworkTest
//
//  Created by LZH on 14-8-21.
//  Copyright (c) 2014年 LZH. All rights reserved.
//

#import "ViewController.h"
#import <MyToolsWithAssets/ViewController1.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//按钮响应函数
- (IBAction)buttonPressed:(id)sender {
    
    ViewController1 *view1 = [[ViewController1 alloc] init];
    view1.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;   //界面切换动画效果
    [self presentViewController: view1 animated: YES completion: NULL];
}


@end

 

 

快捷键"Command + R"运行

 

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

模拟器版本运行结果

 

bubuko.com,布布扣

 

-- Over --

 

 

 

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

标签:style   blog   http   color   io   os   使用   ar   strong   

原文地址:http://www.cnblogs.com/mylizh/p/3971563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!