UIImageView 作用:[用来进行图片展示]

UIImageView

UIImageView初始化

initWithImage:如果设置frame,图片的size就是 imageView的size

UIImage的初始化方法

1、imageNamed:方法

imageNamed:是UIImage的一个类方法,它做的事情比我们看到的要稍微多一些。它的加载流程如下:

a. 系统会去检查系统缓存中是否存在该名字的图像,如果存在则直接返回。

b. 如果系统缓存中不存在该名字的图像,则会先加载到缓存中,在返回该对象。

  观察上面的操作我们发现系统会缓存我们使用imageNamed:方法加载的图像时候,系统会自动帮我们缓存。这种机制适合于那 种频繁用到界面贴图类的加载,但如果我们需要短时间内频繁的加载一些一次性的图像的话,最好不要使用这种方法。

imageView支持的图片:  首选png,jpg

2、imageWithContentsOfFile:和imageWithData:方法

  图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通 过网络下载一个很大的图像时,请尽量使用imageWithData的方式加载图像。

//两个将图片转换成data的方法(比如:服务器端上传头像,图片缓存数据库)

NSData * __nullable UIImagePNGRepresentation(UIImage * __nonnull image)

//可以进行压缩来操作

NSData * __nullable UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality);

设置圆角和边框颜色

(1)设置圆角

imageView.layer.masksToBounds = YES;

imageView.layer.cornerRadius = 10;

(2)设置边框颜色和大小

imageView.layer.borderColor = [UIColor orangeColor].CGColor;

imageView.layer.borderWidth = 2;

contentMode属性(Demo)

这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:

UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFill

UIViewContentModeRedraw

UIViewContentModeCenter

UIViewContentModeTop

UIViewContentModeBottom

UIViewContentModeLeft

UIViewContentModeRight

UIViewContentModeTopLeft

UIViewContentModeTopRight

UIViewContentModeBottomLeft

UIViewContentModeBottomRight

注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。 UIViewContentModeScaleToFill属性会导致图片变形,图片填充满frame(默认)。

UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。 UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。(有一部分会被裁切掉)

图片拉伸(resizedImage:)(Demo:)

UIImage的图片拉伸方法

stretchableImageWithLeftCapWidth:topCapHeight:

leftCapWidth ,是左侧需要保留的像素数,topCapHeight是顶部需要保留的像素数,然后中间的1像素用于中间的平铺,达到最后所需 要的尺寸。效果相当于只能保持一边固定,拉伸另一边。 

注意:只是对一个像素进行复制到一定宽度。而图像后面的剩余像素也不会被拉伸。

图片动画处理

1.基本动画常用API:

-isAnimation:

-startAnimation:

-stopAnimation:

-setAnimationImages:

-setAnimationDuration:

-setAnimationRepeatCount:

2.使用一张GIF图,完成动画(需要使用CoreText框架,需要导入ImageIO框架)

GIF ==> Data ==> 获取gif帧数(总数量)  ==>   获取指定帧的图像  ==> 获取指定图像的属性  ==> 获取属性中的GIF属性  ==>  找出持续时间  ==>   拿到总时间  ==>  设置iamgeView的基本属性完成动画

//Create an image source reading from `data'

CGImageSourceRef __nullable CGImageSourceCreateWithData(CFDataRef __nonnull data, CFDictionaryRef __nullable options)

/* Return the number of images (not including thumbnails) in the image

* source `isrc'. */

size_t CGImageSourceGetCount(CGImageSourceRef __nonnull isrc)

/* Return the image at `index' in the image source `isrc'.  The index is

zero-based. */

CGImageRef __nullable CGImageSourceCreateImageAtIndex(CGImageSourceRef __nonnull isrc, size_t index, CFDictionaryRef __nullable options)

/* Return the properties of the image source `isrc’.*/

CFDictionaryRef __nullable CGImageSourceCopyProperties(CGImageSourceRef __nonnull isrc, CFDictionaryRef __nullable options)

//获取指定的关于GIF属性:(const CFStringRef kCGImagePropertyGIFDictionary)

*frameProperties = [properties:( *)kCGImagePropertyGIFDictionary];

//获取图片的持续时间(const CFStringRef kCGImagePropertyGIFDelayTime)

NSNumber *lastTime = [frameProperties objectForKey:(NSString *) kCGImagePropertyGIFDelayTime];

注意内存管理:Core核心框架的对象需要使用对应的release方式,ARC无法对Core核心框架的对象做管理

CFRelease(),CGImageRelease(),等

最新文章

  1. java语言程序设计(一)-2
  2. noi题库(noi.openjudge.cn) 1.5编程基础之循环控制T36——T45
  3. HNU 12833 Omar’s Bug(分情况讨论)
  4. ACM学习之路————一个大整数与一个小整数不得不说得的秘密
  5. dedecms5.7怎么取消邮箱验证以及dedecms 会员发布的文章不需要审核的解决方法
  6. C#对象XML序列化
  7. Mac IDEA快捷键积累
  8. Linux内核启动分析
  9. WebView使用input file控件打开相册上传图片
  10. 了解 : 多个Http请求设计方向 (batch)
  11. Log4j – Log4j 2 API
  12. windows下JDK环境配置与Android SDK环境配置
  13. 数组中的逆序对(python)
  14. 20165336 2017-2018-2 《Java程序设计》第3周学习总结
  15. 操作系统IIS安装
  16. [转]什么是VUE?为什么用VUE?
  17. 全国Uber优步司机奖励政策 (1月4日-1月10日)
  18. sgu 131 状压DP
  19. phpwind主要表结构的研究随笔[1]
  20. Mustache(2)

热门文章

  1. drf
  2. Ubuntu18.04.3 LTS初体验
  3. C# 获取系统当前登录用户(管理员身份运行同样有效)
  4. pat 1015 Reversible Primes(20 分)
  5. nyoj 76-超级台阶 (递推)
  6. systemd概念和运行机制
  7. java中的transient关键字详解
  8. JavaWeb01-常识
  9. LLDB调试详解--逆向开发
  10. 从零开始在ubuntu上配置深度学习开发环境