這篇文章要解決的問題是:如何以一個自訂的 imageNamed 函數來取代系統的 imageNamed 函數。在內部邏輯上,我們將貫穿對比論證「怎樣的圖片才算合適」。如果你對 iOS 載入圖片的規則不太熟悉,建議重點閱讀這篇。
以 iPhone 7 plus(iOS 10.0)為例,圖片載入優先級為:sample@3x.png > sample@2x.png > sample~iphone.png > sample.png,其他後綴的圖片總是不會被載入。
以 iPad Pro 12.9 吋(iOS 10.0)為例,優先級為:sample@2x.png > sample~ipad.png > sample.png,其他後綴的圖片同樣不會被載入。
可以透過同名不同內容的圖片來對比觀察,優先級從高到低,優先級較高的會優先被載入,而優先級為 0 的永遠不會被載入。僅以 iPhone 7 plus 與 iPad Pro 為例分析,其他情況可自行推廣。所用驗證版本為 iOS 10,未來不同機型與系統可能會有差異。
想自己動手試一下的話,可以下載範例專案,檔案很小,只有 100 多 K,用 Xcode 8 即可編譯。

把資源放到一個 bundle 包中,便於保留資源的目錄結構,也方便整體管理與替換。iOS 中的 bundle 包,就是一個以 .bundle 結尾的特殊文件夾。範例中使用的是 main.bundle。另外,關於 bundle 保留資源目錄結構這個特點,是 react-native 中很依賴的一個特性,未來你的專案或許也會需要。如果只是單純從原本的 Images.xcassets 遷移程式碼,此處放在同一層級即可。
把圖片放到資源文件夾 main.bundle 後,可以參考下面的程式碼來載入圖片。這樣做的好處之一,是能適度減少圖片載入的記憶體占用:
NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"main.bundle"]; NSBundle *mainBundle = [NSBundle bundleWithPath:bundlePath]; NSString *imgPath = [mainBundle pathForResource:@"sample" ofType:@"png"]; self.sampleImageView.image = [UIImage imageWithContentsOfFile:imgPath];
首先需要顯式載入 @3x 的圖片:
NSString *imgPath = [mainBundle pathForResource:@"sample@3x" ofType:@"png"];
此時程式碼在 iPhone 7、iPhone 7 plus、iPad Pro 上都能顯示圖片,直接輸出的圖片尺寸都為原圖的 1/3:
NSLog(@"載入後的圖片尺寸:%@", [NSValue valueWithCGSize:self.sampleImageView.image.size]);
但這裡有個問題:@3x 總是被解讀為三倍圖。在 iPhone 上這正是我們需要的尺寸,但在 iPad 上尺寸就偏小了。我們在 iPad 上通常總希望把這張圖當作 @2x 來顯示——這是一個規律!做過 iPhone 與 iPad 通用圖標尺寸適配的開發者,應該早就注意到了。
所以現在要解決的關鍵技術問題是:如何把 @3x 圖在 iPad 上按照 @2x 來解讀?相對完整的程式碼如下,最終輸出的圖片尺寸在 iPhone 上為原圖的 1/3,在 iPad 上為原圖的 1/2,正是我們需要的:
NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"main.bundle"];
NSBundle *mainBundle = [NSBundle bundleWithPath:bundlePath];
NSString *imgPath = [mainBundle pathForResource:@"sample@3x" ofType:@"png"];
UIImage *image;
static NSString *model;
if (!model) { model = [[UIDevice currentDevice] model]; }
if ([model isEqualToString:@"iPad"]) {
NSData *imageData = [NSData dataWithContentsOfFile:imgPath];
image = [UIImage imageWithData:imageData scale:2.0];
} else {
image = [UIImage imageWithContentsOfFile:imgPath];
}
self.sampleImageView.image = image;
NSLog(@"載入後的圖片尺寸:%@", [NSValue valueWithCGSize:self.sampleImageView.image.size]);
這裡實作了一個簡單夠用的類目方法,支援從指定 bundle 讀取指定名稱的圖片:
#import "UIImage+imageNamed_bundle_.h"
@implementation UIImage (imageNamed_bundle_)
+ (UIImage *)imageNamed:(NSString *)imgName bundle:(NSString *)bundleName {
bundleName = [NSString stringWithFormat:@"%@.bundle", bundleName];
imgName = [NSString stringWithFormat:@"%@@3x", imgName];
NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:bundleName];
NSBundle *mainBundle = [NSBundle bundleWithPath:bundlePath];
NSString *imgPath = [mainBundle pathForResource:imgName ofType:@"png"];
UIImage *image;
static NSString *model;
if (!model) { model = [[UIDevice currentDevice] model]; }
if ([model isEqualToString:@"iPad"]) {
NSData *imageData = [NSData dataWithContentsOfFile:imgPath];
image = [UIImage imageWithData:imageData scale:2.0];
} else {
image = [UIImage imageWithContentsOfFile:imgPath];
}
return image;
}
@end
借助類目,原本的呼叫可以簡化為:
UIImage *image = [UIImage imageNamed:@"sample" bundle:@"main"]; self.sampleImageView.image = image;
它也支援有層級結構的圖片資源讀取:
UIImage *image = [UIImage imageNamed:@"sub/sample" bundle:@"main"]; self.sampleImageView.image = image;
透過這個自訂的類目方法,你不僅能靈活控制圖片的載入來源與尺寸解讀,也為後續的資源熱更新打下了基礎。