美文网首页
swift中UIImageView的使用

swift中UIImageView的使用

作者: 不安分心 | 来源:发表于2016-12-19 14:31 被阅读0次

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
    }
}

相关文章

网友评论

      本文标题:swift中UIImageView的使用

      本文链接:https://www.haomeiwen.com/subject/jvdtvttx.html