一、本地图片
//声明一个imageView,初始化图片
let imageView =UIImageView(image:UIImage(named:"image1"))
//设置尺寸大小
imageView.frame =CGRect(x:10, y:30, width:300, height:150)
//添加到view上
self.view.addSubview(imageView)
//防止拉抻,自动适应
imageView.contentMode = .scaleAspectFit
二、网络图片
let url =URL(string:"http://hangge.com/blog/images/logo.png")
//从网络获取数据流
let data = try!Data(contentsOf: url!)
//通过数据流初始化图片
let newImage =UIImage(data: data)
let imageView =UIImageView(image:newImage);
self.view.addSubview(imageView)
三、从文件目录中获取图片
let path =Bundle.main.path(forResource:"ball", ofType:"png")
let newImage =UIImage(contentsOfFile: path!)
let imageView =UIImageView(image:newImage)
self.view.addSubview(imageView)
四、使用图像控件实现动画播放
var imageView:UIImageView!
imageView =UIImageView()
imageView.frame=CGRect(x:20, y:20, width:100, height:100)
//设置动画图片
imageView.animationImages = [UIImage(named:"icon1")!,UIImage(named:"icon2")!]
//设置每隔0.5秒变化一次
imageView.animationDuration=0.5
self.view.addSubview(imageView)
//开始动画
imageView.startAnimating()
//结束动画
imageView.stopAnimating()