UIImageView的使用
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 創(chuàng)建imageView
let imageView = UIImageView(image: UIImage(named: "iconimage"))
imageView.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
imageView.backgroundColor = UIColor.red
self.view.addSubview(imageView)
// 保持圖片比例
imageView.contentMode = .scaleAspectFit
// 加載本地圖片,從文件目錄下獲取圖片
let path = Bundle.main.path(forResource: "航空意外年度險", ofType: "png")
let newImage = UIImage(contentsOfFile: path!)
// imageView.image = newImage
// MARK: 從網(wǎng)絡(luò)地址獲取圖片
// 1. 定義NSURL對象
let url = NSURL(string: "http://hangge.com/blog/images/logo.png")
// 2. 從網(wǎng)絡(luò)獲取數(shù)據(jù)流
let data = NSData(contentsOf: url! as URL)
// 3. 獲取image
let otherImage = UIImage(data: data as! Data)
imageView.image = otherImage
}
}