前言
當 iOS 程序到一定程度后,種種原因,想要解耦,組件化或者模塊化。這里繞不開的就是基礎模塊獨立,其他模塊依賴基礎模塊。
有的時候,就需要把資源單獨拿出來。可能會有各種方法。這里將要說的是如何用 cocoapods 管理圖片資源和字體庫。
大體上的流程是這樣的:
- 用 cocoapods 創(chuàng)建私庫。
- 提取項目里的資源文件(這里指圖片資源和字體庫)到 cocoapods 中,配置 spec。
- 上傳庫到公司內部服務器。
- 項目里 pod 該庫
cocoapods 創(chuàng)建私庫
創(chuàng)建私庫過程和創(chuàng)建庫過程一致,只是在 spec 配置文件里,將連接地址填寫為私有服務器地址。
創(chuàng)建 pods 庫見官方文檔
pods 庫創(chuàng)建初始化:pod lib create ZLCResources. 根據(jù)提示輸入內容。

創(chuàng)建成功后,會自動彈出工程界面。工程如下:

配置 ZLCResources.podspec
因為要用 pods 管理圖片資源和字體庫,所以我們要告訴 pods ,資源文件在哪。
又因為我們要創(chuàng)建私庫,鏈接地址并不在 github 上,所以需要修改指定的鏈接地址,使其指向我們的服務器的地址。
同時,我們想要支持 iOS 7,所以我們要指定版本。
具體的如下所示:
#鏈接地址指向我們的服務器,這里假設我的地址在coding.net上
s.homepage = 'https://git.coding.net/zlanchun/ZLCResources.git'
s.source = { :git => 'https://git.coding.net/zlanchun/ZLCResources.git', :tag => s.version.to_s }
#指定支持的版本,這里支持 iOS 7.0 及以上的版本
s.ios.deployment_target = '7.0'
#我在ZLCResources/ZLCResources下面創(chuàng)建了2個新文件夾:Iconfonts 和 Images。一個放圖片,一個放字體庫。然后把bundle地址指向這2個目錄。
s.resource_bundles = {
'ZLCResources' => ['ZLCResources/Images/*.*','ZLCResources/Iconfonts/*.*']
}
最后修改完后的 podspec 文件內容:
#
# Be sure to run `pod lib lint ZLCResources.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'ZLCResources'
s.version = '0.1.0'
s.summary = 'ZLCResources for images and iconfonts'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://git.coding.net/zlanchun/ZLCResources.git'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'zlanchun' => 'zlanchun@gmail.com' }
s.source = { :git => 'https://git.coding.net/zlanchun/ZLCResources.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '7.0'
s.source_files = 'ZLCResources/Classes/**/*'
s.resource_bundles = {
'ZLCResources' => ['ZLCResources/Images/*.*','ZLCResources/Iconfonts/*.*']
}
s.public_header_files = 'ZLCResources/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
然后,在 ZLCResources 里創(chuàng)建2個分類,用于提供加載 pods 里的圖片和字體庫用。
加載圖片的分類:
+ (UIImage *)zlc_imageNamed:(NSString *)name ofType:(nullable NSString *)type {
NSString *mainBundlePath = [NSBundle mainBundle].bundlePath;
NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",mainBundlePath,@"ZhuanResourcesBundle.bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
if (bundle == nil) {
bundlePath = [NSString stringWithFormat:@"%@/%@",mainBundlePath,@"Frameworks/ZhuanResources.framework/ZhuanResourcesBundle.bundle"];
bundle = [NSBundle bundleWithPath:bundlePath];
}
if ([UIImage respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)]) {
return [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
} else {
return [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:type]];
}
}
加載字體庫的分類:
#import "UIFont+ZLCFontLoader.h"
#import <CoreText/CTFontManager.h>
implementation UIFont (ZLCFontLoader)
+ (UIFont *)iconFontOfSize:(CGFloat)size
{
NSString *fontName = @"unsplashFont";
UIFont *font = [UIFont fontWithName:fontName size:size];
if (!font) {
[[self class] dynamicallyLoadFontNamed:fontName];
font = [UIFont fontWithName:fontName size:size];
if (!font) font = [UIFont systemFontOfSize:size];
}
return font;
}
+ (void)dynamicallyLoadFontNamed:(NSString *)name
{
NSString *fontfileName = @"iconfont.ttf";
NSString *mainBundlePath = [NSBundle mainBundle].bundlePath;
NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",mainBundlePath,@"ZLCResources.bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
if (bundle == nil) {
bundlePath = [NSString stringWithFormat:@"%@/%@",mainBundlePath,@"Frameworks/ZLCResources.framework/ZLCResources.bundle"];
bundle = [NSBundle bundleWithPath:bundlePath];
}
NSString *resourcePath = [NSString stringWithFormat:@"%@/%@",bundle.bundlePath,fontfileName];
NSURL *url = [NSURL fileURLWithPath:resourcePath];
NSData *fontData = [NSData dataWithContentsOfURL:url];
if (fontData) {
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)fontData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
}
}
@end
使用:
//ZLCViewController 里,引入頭文件
#import "ZLCViewController.h"
#import <ZLCResources/UIImage+ZLCImageLoader.h>
#import <ZLCResources/UIFont+ZLCFontLoader.h>
//viewdidload 里調用字體庫和圖片資源
self.iconFontLabel.font = [UIFont iconFontOfSize:24];
self.iconFontLabel.text = @"Download \U0000e607";
self.imageView.image = [UIImage zlc_imageNamed:@"wallpaper83.5" ofType:nil];
注意
字體庫的名字,不是文件的名字,而是Font Family。這里容易混。

Podfile 里,use_frameworks! 表示是否生成 framework。如果沒有 use_frameworks!,則表示bundle直接生成在工程當前目錄下,和mainBundle 是一個目錄地址。如果有,則表示生成在當前 framwork 的目錄下。
看圖:


最后
都修改完后:
- pod lib lint ,檢驗spec是否合乎規(guī)則
- pod install ,重新安裝修改該后的本地庫
然后,驗證通過后,將工程上傳到服務器上。
新建一個工程,在 Podflie 里集成新創(chuàng)建的庫,并指定git地址,測試效果。
pod "ZLCResources", :git => 'https://git.coding.net/zlanchun/ZLCResources.git'