imageNamed:
官方解釋
Returns the image object associated with the specified filename.
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. If a matching image object is not already in the cache, this method locates and loads the image data from disk or from an available asset catalog, and then returns the resulting object.
The system may purge cached image data at any time to free up memory. Purging occurs only for images that are in the cache but are not currently being used.
In iOS 9 and later, this method is thread safe.
翻譯:根據(jù)指定的文件名返回相關(guān)的image對(duì)象。
該方法在系統(tǒng)緩存中查找具有指定名稱的圖像對(duì)象,并返回最適合主屏幕的該圖像的變體。 如果緩存中沒有找到相應(yīng)的圖片,這個(gè)方法從指定的文檔中加載然后緩存并返回這個(gè)對(duì)象。
系統(tǒng)可以隨時(shí)清除緩存的圖像數(shù)據(jù),以釋放內(nèi)存。 清除僅針對(duì)緩存中的圖像而不是當(dāng)前正在使用的。
在iOS 9及更高版本中,此方法是線程安全的。
imageWithContentsOfFile:
Creates and returns an image object by loading the image data from the file at the specified path.
This method does not cache the image object.
翻譯:通過從指定路徑的文件加載圖像數(shù)據(jù)來創(chuàng)建并返回圖像對(duì)象。
此方法不緩存圖像對(duì)象。
總結(jié):
imageNamed:方法在加載切圖時(shí)會(huì)將切圖緩存在系統(tǒng)內(nèi)存中,對(duì)于重復(fù)加載的切圖此方法的性能會(huì)提升很多,用戶體驗(yàn)會(huì)更好。所以如果某張圖片需要在應(yīng)用中使用多次,或者重復(fù)引用,使用imageName的方式會(huì)更好。
imageWithContentsOfFile:使用時(shí)從沙盒讀取,完成之后系統(tǒng)會(huì)釋放,不會(huì)緩存下來。適用于有些圖片在應(yīng)用中只使用一兩次的,就可以用這樣的方式,比如新手引導(dǎo)界面的圖片等等,就適合這樣的方式。