1、imageNamed
imageNamed的優(yōu)點(diǎn)是可以將已經(jīng)加載過(guò)的圖片緩存到內(nèi)存中。蘋(píng)果的文檔中有如下說(shuō)法:
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method locates and loads the image data from disk or asset catelog, and then returns the resulting object. You can not assume that this method is thread safe.
這個(gè)方法根據(jù)圖片名稱(chēng)會(huì)先在系統(tǒng)緩存中尋找圖片。如果緩存中沒(méi)有這個(gè)圖片對(duì)象,這個(gè)方法會(huì)從指定的文件中尋找對(duì)應(yīng)的圖片,并將其緩存到內(nèi)存中。
當(dāng)你需要重復(fù)使用一張圖片的時(shí)勃痴,比如UITableView里的cell需要重復(fù)加載一張圖片時(shí)
2、imageWithContentsOfFile或者imageWithData
這個(gè)方法只是簡(jiǎn)單的加載圖片热康,并不會(huì)緩存到內(nèi)存中沛申。圖片會(huì)以數(shù)據(jù)加載到程序中
當(dāng)不需要重用圖片,或者是下載一個(gè)很大圖片等可以使用該方法
系統(tǒng)不會(huì)浪費(fèi)內(nèi)存姐军。