UIImageview and UIImage 之Swift学习

前端之家收集整理的这篇文章主要介绍了UIImageview and UIImage 之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, 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!)

//把加载到的图片丢给imageViewimage现实

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

猜你在找的Swift相关文章