一般組件的核心代碼放在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
為圖片文件目錄
回到HFMyTest
的Example
模塊蹂随,我們進(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ì)被放到mainBundle
的Frameworks
文件夾下的熙侍,下面進(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!
- 使用
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
- 使用
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)建bundle
(Podfile
中未使用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)了圖片在
mainBundle
的MyImg.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