/* swift 3.0 UIImageView的使用
*/
var imageView = UIImageView.init(image: UIImage.init(named: "a.png"))
imageView.center = view.center
view.addSubview(imageView)
/* 图片缩放策略 一般用scaleAspectFit
case scaleToFill /* 拉伸,充满背景 */
case scaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent /* 拉伸,不过是保证width充满,之后height等比拉伸 */
case scaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped./* 拉伸,保证小的先满足,再等比拉伸 */
重绘,没用过
case redraw // redraw on bounds change (calls -setNeedsDisplay)
下面都是图不拉伸,且显示的位置
case center // contents remain same size. positioned adjusted.
case top
case bottom
case left
case right
case topLeft
case topRight
case bottomLeft
case bottomRight
*/
imageView.contentMode = .scaleAspectFit
/* 图片圆角 */
imageView.layer.masksToBounds = true;
imageView.layer.cornerRadius = 10;
/* 从本地加载图片
1. imageNamed方式:
此时加载的图片是带有缓存的
而且此时系统能自动加载对应分辨率的图片
2. contentsOfFiles方式:
此时图片不会被缓存
此时需要自己设置一个字符串,去拼接图片,从而显示对应分辨率的图片
虽然第二种方式看上去没那么方便,但是不带缓存,内存占用少;
那么该如何选择了?
(1)不滑动,不能移动的页面 采用 contentsOfFiles,
(2)tableview,scrollView,collectionView等,需要快速滑动的 采用 imageNamed
具有动态的页面,各种资源加载越快越好,所以采用缓存策略;
而静态的页面,对fps影响不大,所以采用不带缓存的加载策略;
*/
/* imageNamed */
imageView.image = UIImage.init(named: "a.png")
/* contentOfFiels */
let path = Bundle.main.path(forResource: "b", ofType: "png")
let newImage = UIImage.init(contentsOfFile: path!)
imageView.image = newImage
/* 图片帧动画 */
imageView.animationImages = [UIImage.init(named: "a.png")!,UIImage.init(named: "b.png")!]
imageView.animationDuration = 0.5
imageView.startAnimating()
imageView.animationRepeatCount = 1000
// open func stopAnimating() 停止动画
// open var isAnimating: Bool { get } 动画是否正在进行