【GeekBand】UIImage显示图片

UIImage

  • 通过属性显示assets中的图片
  • 通过代码显示图片

显示图片最直接的方式就是使用Image View控件。
在工具箱中拖拽Image View控件到需要的View Controller中。

ImageView

选中Image View控件,可在右侧Inspector的Image下拉框,下拉选中图片。
选择Image

下拉框中显示的图片是来自Assets.xcassets文件夹的,因此你需要先将图片拖进assets。

添加图片

Image View支持大部分图片格式,但官方推荐png和jpeg两种格式。

通过代码显示图片

通过Inspector显示图片有一定局限性,比如图片没有放到assets中就不能选择了。因此应该掌握通过代码显示图片的方法。
代码显示图片的基本方法都是创建一个UIImage对象,然后将该对象赋值给Image View控件的image属性。

    UIImage *localImage = [UIImage imageNamed:@"imagename"];
    imageView.image = localImage; //imageView是与Image View控件关联的属性 

这里的关键是如何创建UIImage对象,针对不同的场景有不同的方式。下图是Apple文档说明:

Use the imageNamed:inBundle:compatibleWithTraitCollection: method (or the imageNamed: method) to create an image from an image asset or image file located in your app’s main bundle (or some other known bundle). Because these methods cache the image data automatically, they are especially recommended for images that you use frequently.

Use the imageWithContentsOfFile: or initWithContentsOfFile: method to create an image object where the initial data is not in a bundle. These methods load the image data from disk each time, so you should not use them to load the same image repeatedly.

Use the animatedImageWithImages:duration: and animatedImageNamed:duration: methods to create a single UIImage object comprised of multiple sequential images. Install the resulting image in a UIImageView object to create animations in your interface.

这里介绍一种显示动画的方法。
将一些列的图片按顺序命名成animate1.png,animate2.png,animate3.png...。然后拖到assets中。然后在viewDidLoad中编写如下代码:

    UIImage *localImage = [UIImage animatedImageNamed:@"animate", duration:1.0];
    imageView.image = localImage; //imageView是与Image View控件关联的属性

效果就是Image View控件每秒显示一张新的图片,并且不断循环,这样就起到了动画的效果。

May-25-2016 18-07-06.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,976评论 25 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,498评论 0 17
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 重磅来袭,中移动188不限量套餐: 1人付费,全家共享(1托4)。流量不限量,还送1000分钟国内通话,1000分...
    湾仔城天街阅读 155评论 0 0
  • vhjhhuisnwbbjjsjbbdbjsjj
    仇志轩阅读 122评论 0 5