第二章 Drawing

​ 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:.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容