使用 Swift 繪制圓形頭像宴倍,代碼如下:
//
// ViewController.swift
// CircleFavicon
//
// Created by chenyu on 2016/10/29.
// Copyright ? 2016年 ChenYu. All rights reserved.
//
/*
圖片原本是矩形播掷,回執(zhí)成圓形頭像
*/
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//第一種
let circleFavicon1 = UIImageView.init(frame: CGRect.init(x: 100, y: 100, width: 50, height: 50))
circleFavicon1.image = self.circleFaviconWithFromQuqrtz2D(name: "qiqi.JPG", borderWidth: 2.0, borderColor: .purple)
self.view.addSubview(circleFavicon1)
self.view.backgroundColor = UIColor.darkGray
//第二種
let circleFavicon2 = UIImageView.init(frame: CGRect.init(x: 100, y: 200, width: 50, height: 50))
circleFavicon2.image = UIImage.init(named: "qiqi.JPG")
self.circleFaviconFromCALayer(imageView: circleFavicon2)
self.view.addSubview(circleFavicon2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/// Swift2.3以前
/// 使用 quartz2D 繪制圓形頭像
///
/// - parameter name: 圖片名字
/// - parameter borderWidth: 繪制后的圖片邊框
/// - parameter borderColor: 邊框顏色
///
/// - returns: 繪制后的圖片
func circleFaviconWithFromQuqrtz2D(name: String, borderWidth: CGFloat, borderColor: UIColor) -> UIImage {
//加載原圖
let original = UIImage.init(named: name)
//開啟上下文
let imageW: CGFloat = (original?.size.width)! + 22 * borderWidth
let imageH: CGFloat = (original?.size.height)! + 22 * borderWidth
let imageSize = CGSize.init(width: imageW, height: imageH)
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)
//取得當(dāng)前的上下文,這里得到的就是上面剛創(chuàng)建的圖片上下文
let context = UIGraphicsGetCurrentContext()
//畫邊框(大圓)
borderColor.set()
let bigRadius: CGFloat = imageW * 0.5 //大圓半徑
let centerX = bigRadius //圓心
let centerY = bigRadius //圓心
let center = CGPoint.init(x: centerX, y: centerY)
let endAngle = CGFloat(M_PI*2)
context?.addArc(center: center, radius: bigRadius, startAngle: 0, endAngle: endAngle, clockwise: false)
context?.fillPath()
//小圓
let smallRadius = bigRadius - borderWidth
context?.addArc(center: center, radius: smallRadius, startAngle: 0, endAngle: endAngle, clockwise: false)
context?.clip()
//畫圓
original?.draw(in: CGRect.init(x: borderWidth, y: borderWidth, width: (original?.size.width)!, height: (original?.size.height)!))
//取圖
let circleFavicon = UIGraphicsGetImageFromCurrentImageContext()
//結(jié)束上下文
UIGraphicsEndImageContext()
return circleFavicon!
}
/// 使用 CALayer 繪制圓形頭像
///
/// - parameter imageView: 要被繪制的 imageView
func circleFaviconFromCALayer(imageView: UIImageView) -> Void {
let imageView = imageView
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = imageView.bounds.size.width * 0.5
imageView.layer.borderWidth = 5.0
imageView.layer.borderColor = UIColor.white.cgColor
}
}
Swift2.3 版本的寫法(寫在 UIImage 的擴展中)
/**
* param: radius 圓角半徑
* 注意:只有當(dāng)imageView.image不為nil時五垮,調(diào)用此方法才有效果
*/
func cornerRadius(bounds: CGRect ,radius:CGFloat) -> UIImage{
//開始圖形上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(bounds.size.width, bounds.size.height), false, UIScreen.mainScreen().scale)
//獲取圖形上下文
let ctx = UIGraphicsGetCurrentContext()
//根據(jù)一個rect創(chuàng)建一個橢圓
CGContextAddEllipseInRect(ctx!, bounds)
//裁剪
CGContextClip(ctx!)
//將原照片畫到圖形上下文
self.drawInRect(bounds)
//從上下文上獲取剪裁后的照片
let newImage = UIGraphicsGetImageFromCurrentImageContext()
//關(guān)閉上下文
UIGraphicsEndImageContext()
return newImage!
}
Swift3.0 版本,(寫在 UIImageView 的擴展中)
/**
* param: radius 圓角半徑
* 注意:只有當(dāng)imageView.image不為nil時抠忘,調(diào)用此方法才有效果
*/
func cornerRadius(radius:CGFloat){
//開始圖形上下文
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
//獲取圖形上下文
let ctx = UIGraphicsGetCurrentContext()
//根據(jù)一個rect創(chuàng)建一個橢圓
ctx!.addEllipse(in: self.bounds)
//裁剪
ctx!.clip()
//將原照片畫到圖形上下文
self.image!.draw(in: self.bounds)
//從上下文上獲取剪裁后的照片
let newImage = UIGraphicsGetImageFromCurrentImageContext()
//關(guān)閉上下文
UIGraphicsEndImageContext()
self.image = newImage
}
}
Swift 一版一點變化,有小變化也有大變化秧秉,真的要哭了褐桌。。象迎。荧嵌。