iOS開發之靜態庫(三)—— 圖片、介面xib等資源檔封裝到.a靜態庫

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   strong   

 

編譯環境:Macbook Air + OS X 10.9.2 + XCode5.1 + iPhone5s(iOS7.0.3)

 

一、首先將資源檔打包成bundle

建立工程:File -> New -> Project... -> OS X -> Framework & Library -> Bundle

我們必須藉助於OS X,因為iOS架構中沒有建立Bundle的模板

填寫項目名稱

儲存位置

       其實本無需這麼麻煩,bundle的本質是一個目錄,我們完全可以建立一個檔案夾,把需要封裝的資源檔拷貝至該目錄下,將其改成尾碼名為bundle即可,然後就可以使用NSBundle提供的介面訪問其內部的資源檔,當然,也可以通過訪問普通檔案目錄的方法訪問其內部的資源。

       此處主要是考慮到有xib檔案的存在,由前面的知識可知,xib是文字檔,編譯後要被序列化為二進位的nib檔案,使用時將nib檔案還原序列化,就可以正常顯示介面了。而bundle本身是靜態,其內部的資源套件不參與項目的編譯,所以,此處必須建立工程把xib序列化為二進位的nib,否則的話,直接建立檔案夾後改名就可以了。

       因此一些輔助檔案(紅色矩形框起的部分)都不需要了,直接刪除即可

有一點一定要十分十分的小心:

刪除上面紅色矩形框中檔案時,可以完全的刪除,因為這些檔案都是我們自己建立的

        而刪除下面矩形框中的檔案時,千萬不能“Move to Trash”,只能"Remove References"(刪除引用),因為framework是系統架構的一部分,如果刪除了,下次運行時候,就會報錯。我就犯過這個低級錯誤,從來沒想過系統檔案會被自己刪除,所以也沒往這方面去想,以至於花費了很長時間才解決了問題。

由於我們刪除了plist檔案,所以必須在Build Settings中刪除對它的引用

同樣,也要刪除pch檔案的引用

我們建立項目時選的是OS X中的架構,所以此處需要修改Base SDK為iOS

修改後

設定完畢,現在拷貝資源檔到項目中來

正是我們需要的兩個圖片檔案,一個xib檔案

一切就緒,按快速鍵"Command + B"編譯,編譯成功後,Products下面的MyToolsWithAssetsA.bundle由紅色變成了黑色,表示編譯成功

找到產生的bundle所在的目錄

查看裡面的內容

開啟

正是我們所需要的,xib也被轉化成了nib

 

二、編譯靜態庫

建立靜態庫

       通常,bundle的名稱要和對應的靜態庫或framework相同(也可以不相同),所以此處靜態庫也命名為“MyToolsWithAssetsA”,這就需要和前面的bundle工程存放在不同的目錄,當然,如果不想這麼麻煩,也可以開始給bundle工程起一個其他名字,使用時修改一下。

刪除不需要的檔案

加入我們的檔案

 

#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
////  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

  

本項目中使用了兩張圖片,一張是直接在xib檔案中指定,另一張手動添加,並從bundle中擷取圖片資源

"Command + B"編譯,分別產生針對模擬器的靜態庫和針對真機的靜態庫

 

合并

 

三、建立測試工程

建立iOS下的單視圖模板

工程命名

匯入靜態庫、標頭檔和bundle資源套件

在主介面Main.storyboard上增加一個Button,其響應函數代碼如下:

//按鈕響應函數- (IBAction)buttonPressed:(id)sender {        ViewController1 *view1 = [[ViewController1 alloc] init];    view1.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;   //介面切換動畫效果    [self presentViewController: view1 animated: YES completion: NULL];}

  

說明:

       我們練習對圖片,xib等資源的封裝,而測試工程卻使用的是storyboard,看起來有點混亂,主要是從XCode5.0開始,所建立的項目介面都被預設的設定成了storyboard,此處也不想去折騰了,意思表達清楚就OK了,偶爾來點混搭也是不錯滴。

"Command + R"編譯運行,運行結果

 

 

大功告成!

 

 

iOS開發之靜態庫(三)—— 圖片、介面xib等資源檔封裝到.a靜態庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.