
本文主要講imageNamed與imageWithContentsOfFile的差異,需要注意的點(diǎn),與實(shí)戰(zhàn)中遇到的坑。
好久沒(méi)寫(xiě)過(guò)博客了,什么工作太忙,加班太晚我就不說(shuō)了,都怪自己太懶??,時(shí)間都是擠出來(lái)的??粗魑淮笈?xiě)的文章,簡(jiǎn)直過(guò)癮,希望有一天自己也能寫(xiě)出這么高質(zhì)量、干貨密集的文章,先從簡(jiǎn)單的做起吧。
從差異說(shuō)起
從磁盤(pán)加載圖片,UIImage主要提供了兩種方式:
+(UIImage *)imageNamed:(NSString *)name;
+(UIImage *)imageWithContentsOfFile:(NSString *)path;
關(guān)于這兩種方法的使用時(shí)機(jī),蘋(píng)果官方文檔描述如下:
Use the imageNamed:inBundle:compatibleWithTraitCollection: method (or the imageNamed: method) to create an image from an image asset or image file located in your app’s main bundle (or some other known bundle). Because these methods cache the image data automatically, they are especially recommended for images that you use frequently.
Use the imageWithContentsOfFile: or initWithContentsOfFile: method to create an image object where the initial data is not in a bundle. These methods load the image data from disk each time, so you should not use them to load the same image repeatedly.
也就是說(shuō),imageNamed:第一次加載圖片時(shí)會(huì)緩存圖片到內(nèi)存,適合使用頻繁的圖片,imageWithContentsOfFile:不會(huì)把圖片緩存到內(nèi)存,每次調(diào)用都要重新從磁盤(pán)加載一次。
在實(shí)際使用中我們要根據(jù)業(yè)務(wù)來(lái)判斷調(diào)用具體的方法,來(lái)最優(yōu)化內(nèi)存與性能。舉個(gè)例子:
- 登陸背景圖,只會(huì)在用戶(hù)登陸的時(shí)候使用,而且圖片較大,就建議用
imageWithContentsOfFile:加載; - 底導(dǎo)航圖標(biāo),圖標(biāo)較小,使用頻繁,就建議使用
imageNamed:加載;
imageNamed:方法還有個(gè)限制,它是在main bundle里找圖片,如果圖片放在Images.xcassets或者直接把圖片方在工程里,參數(shù)直接傳圖片名可以找到。像我司的圖片是放在單獨(dú)建立的bundle里,如果要用imageNamed:加載的話(huà)文件名前面就要加上bundle名,像這樣a.bundle/b.png。
屏幕適配問(wèn)題
iOS的圖片文件需要提供3種尺寸的1x、2x、3x,根據(jù)不同的屏幕尺寸我們需要加載不同的圖片,關(guān)于不同屏幕的圖片加載,蘋(píng)果已經(jīng)幫我們封裝好了,我們只需要將3中尺寸的圖片放到工程中,然后調(diào)用imageNamed:或者imageWithContentsOfFile:,它會(huì)自動(dòng)根據(jù)屏幕尺寸來(lái)加載不同的圖片。
關(guān)于imageNamed:,官方文檔中有這么一段討論:
This method looks in the system caches for an image object with the specified name and returns the variant of that image that is best suited for the main screen.
imageWithContentsOfFile:還沒(méi)找到官方文檔的說(shuō)明(如果各位知道,歡迎各位大牛在評(píng)論中提出),不過(guò)我測(cè)試過(guò)是可以的。
使用imageWithContentsOfFile的一個(gè)坑
在使用imageWithContentsOfFile:加載圖片的時(shí)候遇到一個(gè)坑,先上代碼:
+ (UIImage *)imageWithName:(NSString *)name type:(NSString *)type inBundle:(NSString *)bundle {
NSString *imageBundlePath = [[NSBundle mainBundle] pathForResource:bundle ofType:@"bundle"];
NSBundle *imageBundle = [NSBundle bundleWithPath:imageBundlePath];
NSString *imagePath = [imageBundle pathForResource:name ofType:type];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
return image;
}
很簡(jiǎn)單的一個(gè)函數(shù),就是獲取bundle全路徑,然后再獲取到bundle里圖片的全路徑,然后調(diào)用imageWithContentsOfFile:加載圖片。在使用的時(shí)候也很正常,但是有一天發(fā)現(xiàn)某張圖加載不出來(lái)了。檢查資源文件,只有2x的圖(又是一個(gè)偷懶的程序員。。。很不建議這么玩,雖然只有2x的圖,在所有屏幕都能顯示,但是會(huì)造成圖片的壓縮與放大,每個(gè)細(xì)節(jié)都很重要?。。。?,如果加上1x的圖就可以加載出來(lái)了。
經(jīng)過(guò)調(diào)試發(fā)現(xiàn)問(wèn)題就出在pathForResource:ofType上,這個(gè)函數(shù)是精確匹配調(diào)用者輸入的文件名,不會(huì)自動(dòng)識(shí)別文件名后面的@2x。修改后的代碼:
+ (UIImage *)imageWithName:(NSString *)name type:(NSString *)type inBundle:(NSString *)bundle {
NSString *imageBundlePath = [[NSBundle mainBundle] pathForResource:bundle ofType:@"bundle"];
NSBundle *imageBundle = [NSBundle bundleWithPath:imageBundlePath];
NSString *imageFullName = [name stringByAppendingPathExtension:type];
NSString *imagePath = [[imageBundle resourcePath] stringByAppendingPathComponent:imageFullName];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
return image;
}
歡迎關(guān)注我的博客