//
// ViewController.swift
// UIImageview and UIImage
// Created by Mac on 15/4/12.
// Copyright (c) 2015年 BSY. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 本地图片的加载
// self .addimage()
//加载网络图片(非本地)
self.addRemoteImageView()
}
func addimage()
{
// 初始化uiimageview and uiimage
var uimageview = UIImageView(frame: CGRectMake(35,100,300, 300))
//设置加载一张本地图片
let image = UIImage(named:"cat.jpg")
uimageview.image = image
//把uiimageview加载到父控件上,也就是self.view
self.view.addSubview(uimageview)
}
//加载网络图片(非本地)
func addRemoteImageView()
{
var imageView = UIImageView(frame: CGRectMake(35, 300))
// 图片地址
let strUrl = "http://e.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=410619fb3d01213fdb3e468e358e5db4/9f510fb30f2442a71525d087d543ad4bd11302ec.jpg"
//url
let url = NSURL(string: strUrl)
//图片数据
var data = NSData(contentsOfURL:url!)
//通过得到图片数据来加载
let image = UIImage(data: data!)
//把加载到的图片丢给imageView的image现实
imageView.image = image
//边框颜色设置
imageView.layer.borderColor = UIColor.redColor().CGColor
//边框的宽度设置
imageView.layer.borderWidth = 2
//圆角的设置
imageView.layer.cornerRadius = 150
// 最主要的一句
imageView.layer.masksToBounds = true
//把uiimageview加载到父控件上,也就是self.view
self.view.addSubview(imageView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
原文链接:https://www.f2er.com/swift/327302.html