iOS-組件化加載圖片等資源文件

一般組件的核心代碼放在Classes目錄中嘀韧,而圖片存放于Assets目錄下篇亭,如圖所示,拖動(dòng)部分圖片到Assets中


一锄贷、修改Spec(Podfile中未使用use_frameworks!)

修改HFMyTest.podspec資源加載方式

s.resource_bundles = {
  'HFMyTestImg' => ['HFMyTest/Assets/*']
}

HFMyTestImg為顯示的圖片資源的bundle的名字译蒂,在本地會(huì)顯示為HFMyTestImg.bundle,這個(gè)可以自定義肃叶,HFMyTest/Assets為圖片文件目錄
回到HFMyTestExample模塊蹂随,我們進(jìn)行一次本地的安裝和測(cè)試(pod install)

FFMyView中添加如下代碼

#import "FFMyView.h"

@implementation FFMyView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        [self addSubview:imgView];
        imgView.backgroundColor = [UIColor yellowColor];
        imgView.image = [UIImage imageNamed:@"白富美.png"];
    }
    return self;
}

@end

然后在主控制器FFViewController中調(diào)用FFMyView

#import "FFViewController.h"
#import <HFMyTest/FFMyView.h>

@interface FFViewController ()

@end

@implementation FFViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    FFMyView *imgView = [[FFMyView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.view addSubview:imgView];
    imgView.backgroundColor = [UIColor redColor];
}

@end

運(yùn)行一次項(xiàng)目,發(fā)現(xiàn)圖片不能正常的顯示出來

二因惭、 修改資源加載方式

上面的圖片加載方式為

[UIImage imageNamed:@"圖片名稱"];

在項(xiàng)目中我們打開主包內(nèi)容


右鍵顯示圖片的資源包HFMyTestImg.bundle的包內(nèi)容就可以看見里面圖片資源了


通常如果我們?cè)谥鞴こ陶{(diào)用主工程的資源時(shí)岳锁,可以直接imageName或者[mainbundle pathForResource]讀取,但是在用pod進(jìn)行管理的時(shí)候蹦魔,pod中的資源文件也會(huì)變成bundle加入到mainBundle中激率,但是由于資源文件的bundle并不是mainBundle,所以這種方法是行不通的勿决,關(guān)鍵是要取到資源相關(guān)聯(lián)的bundle

上面圖片可以看出圖片資源處于HFMyTestimg.bundle包下乒躺,所以需要拼接方式關(guān)聯(lián)到該包下

NSURL *bundleURL = [[NSBundle mainBundle]  URLForResource:bundleName withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];

修改項(xiàng)目代碼為

#import "FFMyView.h"

@implementation FFMyView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        [self addSubview:imgView];
        imgView.backgroundColor = [UIColor yellowColor];
        NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"HFMyTestImg" withExtension:@"bundle"];
        if (bundleURL) {
            NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
            NSInteger scale = [[UIScreen mainScreen] scale];
            NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", @"白富美",scale];
            imgView.image = [UIImage imageWithContentsOfFile:[bundle pathForResource:imgName ofType:nil]];
        }
    }
    return self;
}

@end

上面的圖片加載imgName必須使用圖片的完整名稱,如白富美@2x.png

或者下面的代碼

#import "FFMyView.h"

@implementation FFMyView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        [self addSubview:imgView];
        imgView.backgroundColor = [UIColor yellowColor];
        NSString *imgName = [NSString stringWithFormat:@"%@/%@", @"HFMyTestImg.bundle",@"白富美"];
        imgView.image = [UIImage imageNamed:imgName];
    }
    return self;
}

@end

接下來運(yùn)行項(xiàng)目就可以看見圖片了


還看見網(wǎng)上一種加載方式

NSBundle *currentBundle = [NSBundle bundleForClass:[self class]];
//圖片名稱要寫全稱
NSString *patch = [currentBundle pathForResource:@"Group.png" ofType:nil inDirectory:@"wgPersonInfoKit.bundle"];
_imgView.image = [UIImage imageWithContentsOfFile:patch];

因?yàn)樾薷牡牡胤綍?huì)很多低缩,所以把這個(gè)方法抽出來:

新建一個(gè)UIImage的分類

#import <UIKit/UIKit.h>

@interface UIImage (wgBundle)

+ (instancetype)wg_imgWithName:(NSString *)name bundle:(NSString *)bundleName targetClass:(Class)targetClass;

@end
#import "UIImage+wgBundle.h"

@implementation UIImage (wgBundle)

+ (instancetype)wg_imgWithName:(NSString *)name bundle:(NSString *)bundleName targetClass:(Class)targetClass{
    NSInteger scale = [[UIScreen mainScreen] scale];
    NSBundle *curB = [NSBundle bundleForClass:targetClass];
    NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", name,scale];
    NSString *dir = [NSString stringWithFormat:@"%@.bundle",bundleName];
    NSString *path = [curB pathForResource:imgName ofType:nil inDirectory:dir];
    return path?[UIImage imageWithContentsOfFile:path]:nil;
}

@end

在使用的地方

_imgView.image = [UIImage wg_imgWithName:@"Group" bundle:@"wgPersonInfoKit" targetClass:[self class]];

上面資源加載方式為
s.resource_bundles = {
  'HFMyTestImg' => ['HFMyTest/Assets/*']
}
資源加載方式還有另一種
s.resources = ['HFMyTest/Assets/*']

這兩種的區(qū)別在于s.resource_bundles會(huì)自動(dòng)創(chuàng)建HFMyTestImg.bundle包(這個(gè)HFMyTestImg自己自定義的)嘉冒,而s.resources不會(huì)創(chuàng)建bundle曹货,文件會(huì)直接放到目錄下

三、采用s.resources加載資源

s.resources = ['HFMyTest/Assets/*']

pod install重新加載項(xiàng)目讳推,然后采用上面的方式顯示包內(nèi)容

刪除掉之前的緩存記錄顶籽,然后重新運(yùn)行下程序,在進(jìn)入到顯示包內(nèi)容里面

會(huì)發(fā)現(xiàn)包里面的內(nèi)容時(shí)這樣的

和上面不同的是上面的圖片資源全部被自動(dòng)打包到HFMyTestImg.bundle中银觅,這個(gè)里面的圖片資源沒有打包成bundle礼饱,而是直接顯示在主包mainBundle中的,這種方式加載圖片時(shí)可以直接采用imageNamed究驴,同時(shí)也不需要寫出完整的圖片名字

#import "FFMyView.h"

@implementation FFMyView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        [self addSubview:imgView];
        imgView.backgroundColor = [UIColor yellowColor];
        imgView.image = [UIImage imageNamed:@"白富美"];
    }
    return self;
}

@end

需要注意的點(diǎn)

OC工程的Podfile一般是不使用use_frameworks!的镊绪,swift和自己創(chuàng)建的lib庫是默認(rèn)有的,關(guān)于這兩點(diǎn)的差異洒忧,如果Podfile不使用use_frameworks!蝴韭,pod里的資源文件會(huì)被打成bundle放在mainBundle下面,如果使用的話會(huì)被放到mainBundleFrameworks文件夾下的熙侍,下面進(jìn)行詳細(xì)說明下

上面的Podfile采用的是
#use_frameworks!

target 'HFMyTest_Example' do
  pod 'HFMyTest', :path => '../'

  target 'HFMyTest_Tests' do
    inherit! :search_paths

  end
end

并沒有采用use_frameworks!
采用use_frameworks!將在下面進(jìn)行說明

四万皿、采用use_frameworks!

  1. 使用resource_bundles
    修改Podfile文件為
use_frameworks!

target 'HFMyTest_Example' do
  pod 'HFMyTest', :path => '../'

  target 'HFMyTest_Tests' do
    inherit! :search_paths

  end
end
`HFMyTest.podspec`文件為

回到項(xiàng)目主目錄

cd /Users/hf/MyTest/HFMyTest/Example
pod install

更新完成后重新打開HFMyTest.xcworkspace,和之前操作一樣的方式刪除掉緩存文件核行,也可以采用下面方式清除緩存

運(yùn)行程序,然后如上方式一樣打開文件包內(nèi)容蹬耘,會(huì)發(fā)現(xiàn)圖片資源的位置改變了

圖片資源在Frameworks下的HFMyTest.framework下的HFMyTestImg.bundle包中
所以這時(shí)候加載圖片已經(jīng)不再mainBundle中了芝雪,需要指定帶現(xiàn)在的圖片資源目錄中

#import "FFMyView.h"

@implementation FFMyView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        [self addSubview:imgView];
        imgView.backgroundColor = [UIColor yellowColor];
        //到指定目錄
        NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
        bundleURL = [bundleURL URLByAppendingPathComponent:@"HFMyTest"];
        bundleURL = [bundleURL URLByAppendingPathExtension:@"framework"];
        if (bundleURL) {
            NSBundle *imgBundle = [NSBundle bundleWithURL:bundleURL];
            bundleURL = [imgBundle URLForResource:@"HFMyTestImg" withExtension:@"bundle"];
            if (bundleURL) {
                NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
                NSInteger scale = [[UIScreen mainScreen] scale];
                NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", @"白富美",scale];
                imgView.image = [UIImage imageWithContentsOfFile:[bundle pathForResource:imgName ofType:nil]];
            }
        }
    }
    return self;
}

@end
  1. 使用resources

首先pod install程序,重新打開HFMyTest.xcworkspace,然后清除緩存(快捷鍵shift+command+k),運(yùn)行程序综苔,打開查看程序包內(nèi)容

其實(shí)相對(duì)于resource_bundles少了個(gè)打包的bundle惩系,resource_bundles會(huì)自動(dòng)創(chuàng)建一個(gè)bundle

修改FFMyView.m的代碼為

#import "FFMyView.h"

@implementation FFMyView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        [self addSubview:imgView];
        imgView.backgroundColor = [UIColor yellowColor];
        //到指定目錄
        NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
        bundleURL = [bundleURL URLByAppendingPathComponent:@"HFMyTest"];
        bundleURL = [bundleURL URLByAppendingPathExtension:@"framework"];
        if (bundleURL) {
            NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
            NSInteger scale = [[UIScreen mainScreen] scale];
            NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", @"白富美",scale];
            imgView.image = [UIImage imageWithContentsOfFile:[bundle pathForResource:imgName ofType:nil]];
        }
    }
    return self;
}

@end

就可以正常的展示圖片了

正常情況下,對(duì)于采用了resources加載資源的如筛,可以手動(dòng)創(chuàng)建bundle用于存放圖片資源

五堡牡、手動(dòng)創(chuàng)建bundlePodfile中未使用use_frameworks!

右鍵新建文件夾


重命名改文件夾名字為MyImg.bundle

右鍵MyImg.bundle顯示包內(nèi)容,將之前的圖片資源copy到里面去

cd /Users/hf/MyTest/HFMyTest/Example
pod install

重新打開HFMyTest.xcworkspace杨刨,發(fā)現(xiàn)pod中的目錄變化了

清除緩存(shift+command+k)晤柄,然后運(yùn)行程序,再去打開包的內(nèi)容


發(fā)現(xiàn)了圖片在mainBundleMyImg.bundle包下妖胀,這種形式相對(duì)于resource_bundles形式來說芥颈,它把圖片資源同樣打包了,但是在本地資源中赚抡,它顯示形式是MyImg.bundle爬坑,圖片全在bundle里面,而resource_bundles在本地顯示沒有bundle涂臣,直接顯示的是圖片的資源盾计,采用下面的代碼就可以調(diào)用

#import "FFMyView.h"

@implementation FFMyView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        [self addSubview:imgView];
        imgView.backgroundColor = [UIColor yellowColor];
        NSString *imgName = [NSString stringWithFormat:@"%@/%@",@"MyImg.bundle",@"白富美"];
        imgView.image = [UIImage imageNamed:imgName];
    }
    return self;
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子署辉,更是在濱河造成了極大的恐慌族铆,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件涨薪,死亡現(xiàn)場(chǎng)離奇詭異骑素,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)刚夺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門献丑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人侠姑,你說我怎么就攤上這事创橄。” “怎么了莽红?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵妥畏,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我安吁,道長(zhǎng)醉蚁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任鬼店,我火速辦了婚禮网棍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘妇智。我一直安慰自己滥玷,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布巍棱。 她就那樣靜靜地躺著惑畴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪航徙。 梳的紋絲不亂的頭發(fā)上如贷,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音到踏,去河邊找鬼倒得。 笑死,一個(gè)胖子當(dāng)著我的面吹牛夭禽,可吹牛的內(nèi)容都是我干的霞掺。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼讹躯,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼菩彬!你這毒婦竟也來了缠劝?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤骗灶,失蹤者是張志新(化名)和其女友劉穎惨恭,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體耙旦,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡脱羡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了免都。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锉罐。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖绕娘,靈堂內(nèi)的尸體忽然破棺而出脓规,到底是詐尸還是另有隱情,我是刑警寧澤险领,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布侨舆,位于F島的核電站,受9級(jí)特大地震影響绢陌,放射性物質(zhì)發(fā)生泄漏挨下。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一脐湾、第九天 我趴在偏房一處隱蔽的房頂上張望复颈。 院中可真熱鬧,春花似錦沥割、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至衅谷,卻和暖如春椒拗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背获黔。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工蚀苛, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人玷氏。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓堵未,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親盏触。 傳聞我的和親對(duì)象是個(gè)殘疾皇子渗蟹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容