본문 바로가기

iOS

UIImage(contentsOfFile:), UIImage(named:) 차이

UIImage(contentsOfFile:) 메소드는 초기 데이터가 번들에 없는 이미지 객체를 만듭니다. 데이터를 캐싱하지 않기 때문에 반복적으로 이미지를 로드하는데 적합하지 않습니다.

Discussion
This method loads the image data into memory and marks it as purgeable. If the data is purged and needs to be reloaded, the image object loads that data again from the specified path.

https://developer.apple.com/documentation/uikit/uiimage/1624112-init

 

Apple Developer Documentation

 

developer.apple.com

 

반면 UIImage(named:) 메소드는 번들에 있는 이미지로 이미지 객체를 만듭니다. 데이터를 캐싱하기 때문에 반복적으로 사용하는 이미지 객체를 생성할 때 적합합니다.

Discussion
When searching the asset catalog, this method prefers an asset containing a symbol image over an asset with the same name containing a bitmap image. Because the system supports symbol images iOS 13 or later, you may include both types of assets in the same asset catalog. In iOS 12 or earlier, the system automatically chooses the bitmap image.
You can’t use this method to load system symbol images; use the init(systemName:) method instead.
This method checks the system caches for an image object with the name you specify, and returns the variant of that image that’s best suited for the main screen. If a matching image object isn’t in the cache, this method creates the image from an available asset catalog or loads the image from disk. 
The system may purge cached image data at any time to free up memory. Purging occurs only for unused images that are in the cache.
In iOS 9 and later, this method is thread safe.

https://developer.apple.com/documentation/uikit/uiimage/1624146-init

 

Apple Developer Documentation

 

developer.apple.com