UIImageView的使用
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 创建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: 从网络地址获取图片
// 1. 定义NSURL对象
let url = NSURL(string: "http://hangge.com/blog/images/logo.png")
// 2. 从网络获取数据流
let data = NSData(contentsOf: url! as URL)
// 3. 获取image
let otherImage = UIImage(data: data as! Data)
imageView.image = otherImage
}
}