一、本地圖片
//聲明一個imageView,初始化圖片
let imageView =UIImageView(image:UIImage(named:"image1"))
//設(shè)置尺寸大小
imageView.frame =CGRect(x:10, y:30, width:300, height:150)
//添加到view上
self.view.addSubview(imageView)
//防止拉抻樱蛤,自動適應(yīng)
imageView.contentMode = .scaleAspectFit
二收津、網(wǎng)絡(luò)圖片
let url =URL(string:"http://hangge.com/blog/images/logo.png")
//從網(wǎng)絡(luò)獲取數(shù)據(jù)流
let data = try!Data(contentsOf: url!)
//通過數(shù)據(jù)流初始化圖片
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)
四奢米、使用圖像控件實現(xiàn)動畫播放
var imageView:UIImageView!
imageView =UIImageView()
imageView.frame=CGRect(x:20, y:20, width:100, height:100)
//設(shè)置動畫圖片
imageView.animationImages = [UIImage(named:"icon1")!,UIImage(named:"icon2")!]
//設(shè)置每隔0.5秒變化一次
imageView.animationDuration=0.5
self.view.addSubview(imageView)
//開始動畫
imageView.startAnimating()
//結(jié)束動畫
imageView.stopAnimating()