UIImageView
创建UIImageView
let imageView = UIImageView()
imageView.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
self.view.addSubview(imageView)
//通过名字加载图片
//imageView.image = UIImage(named:"用户.png")
//通过路径
/* let path = Bundle.main.path(forResource:"1",ofType:".png")
let image = UIImage(contentsOfFile:path!)
imageView.image = image
*/
//名字加载图片;图片内容加载到内存,下次加载不用再去资源包中读取图片,节省时间
//路径加载图片;不会把图片加载到内容,下次加载需要再次起资源包中读取,节省空间
var arr : [UIImage] = [UIImage]()
for index in 1...5 {
//构造图片的名字
let imageName = "\(index).jpg"
//根据名字初始化图片对象
let image = UIImage(named:imageName)
if let _ = image {
arr.append(image!)
}
}
//将数组赋值给animationImages
imageView.animationImages = arr
//动画执行时间
imageView.animationDuration = 2
//动画的执行次数
imageView.animationRepeatCount = 0
//开始播放
imageView.startAnimating()
//停止播放
//imageView.stopAnimating()