//
// 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")
//把加載好的圖片丟給imageview中的image顯示
uimageview.image = image
//把uiimageview加載到父控件上,也就是self.view
self.view.addSubview(uimageview)
}
//加載網絡圖片(非本地)
func addRemoteImageView()
{
var imageView = UIImageView(frame: CGRectMake(35, 100, 300, 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.
}
}