图像控件(UIImageView)

        // Do any additional setup after loading the view, typically from a nib.
        let img = UIImageView(image: UIImage(named: "58(1)"))
        img.frame = CGRect(x: 0, y: 80, width: 50, height: 200)
        
        self.view.addSubview(img)
        
        //从文件目录中获取图片
        let GPLpath = Bundle.main.path(forResource: "58(1)", ofType: "png")
        let GPLimg = UIImageView(image: UIImage(contentsOfFile: GPLpath!))
        GPLimg.frame = CGRect(x: 0, y: 280, width: 200, height: 100)
        self.view.addSubview(GPLimg)
        
        //从网络地址获取图片
        let url = URL(string: "http://hangge.com/blog/images/logo.png")
        //从网络获取数据流
        let imgData = try! Data(contentsOf: url!)
        
        let GPLimg2 = UIImageView(image: UIImage(data: imgData))
        GPLimg2.frame = CGRect(x: 70, y: 80, width: 50, height: 200)
        //保持图片比例
        GPLimg2.contentMode = .scaleAspectFit
        self.view.addSubview(GPLimg2)

你可能感兴趣的:(图像控件(UIImageView))