標籤:uiimageview uiimage swift教程 swift教學視頻 swift
//
// 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.
}
}
UIImageview and UIImage 之Swift學習