//
// 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
let uimageview = UIImageView(frame: CGRect(x: 30, y: 100, width: 300, height: 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: CGRect(x: 30, y: 100, width: 300, height: 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(contentsOf:url! as URL)
//通过得到图片数据来加载
let image = UIImage(data: data! as Data)
//把加载到的图片丢给imageView的image现实
imageView.image = image
//边框颜色设置
imageView.layer.borderColor = UIColor.red.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.
}
}
网友评论