You can display an image in your interface in a UIView subclass thatknows how to show an image, such as a UIImageView or a UIButton. A pure UIViewis all about drawing, and it leaves that drawing largely up to you;
Images and Image Views
You can also obtain image data in some other way, such as by downloadingit, and transform this into a UIImage。
Image Files
pre-existing image file in your app’s bundle can be obtained through the UIImageinitializer init(named:)。这个方法在两个地方查找图片。
- Asset catalog
We look in the asset catalog for an image set with the supplied name. The name iscase-sensitive.
-
Top level of app bundle
We look at the top level of the app’s bundle for an image file with the supplied name.The name is case-sensitive and should include the file extension; if it doesn’t includea file extension, .png is assumed。
When calling init(named:), an asset catalog is searched before the top level of the app’sbundle. If there are multiple asset catalogs, they are all searched, but the search order isindeterminate and cannot be specified, so avoid image sets with the same name.
A nice thing about init(named:) is that the image data may be cached in memory, andif you ask for the same image by calling init(named:) again later, the cached data maybe supplied immediately. Alternatively, you can read an image file from anywhere inyour app’s bundle directly and without caching, using init(contentsOfFile:), whichexpects a pathname string; you can get a reference to your app’s bundle withNSBundle.mainBundle(), and NSBundle then provides instance methods for gettingthe pathname of a file within the bundle, such as pathForResource:ofType:.