Swift円形のアイコンを描く

5134 ワード

Swiftを使用して円形のアイコンを描画します.コードは以下の通りです.
//
//  ViewController.swift
//  CircleFavicon
//
//  Created by chenyu on 2016/10/29.
//  Copyright © 2016  ChenYu. All rights reserved.
//

/*
           ,       
*/

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)
        
        //        ,                  
        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()
        
        //     
        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
        
    }

}

Swift 2.3バージョンの書き方(UIIMageの拡張に書いてあります)
/**

     * param: radius                

     *   :   imageView.image  nil ,         

     */
   func cornerRadius(bounds: CGRect ,radius:CGFloat) -> UIImage{
        
        //       
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(bounds.size.width, bounds.size.height), false, UIScreen.mainScreen().scale)

        //       
        let ctx = UIGraphicsGetCurrentContext()
        //    rect      
        CGContextAddEllipseInRect(ctx!, bounds)
        //  
        CGContextClip(ctx!)
        //           
        self.drawInRect(bounds)
        //             
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        //     
        UIGraphicsEndImageContext()
        
        return newImage!
    }

Swift 3.0バージョン、(UIImageViewの拡張に書かれています)
/**

     * param: radius                

     *   :   imageView.image  nil ,         

     */
    func cornerRadius(radius:CGFloat){

        //       
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
        //       
        let ctx = UIGraphicsGetCurrentContext()
        //    rect      
        ctx!.addEllipse(in: self.bounds)
        //  
        ctx!.clip()
        //           
         self.image!.draw(in: self.bounds)
        //             
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        //     
        UIGraphicsEndImageContext()

        self.image = newImage
    }
}

Swift 1版が少しずつ変わってきて、小さな変化もあって大きな変化もあって、本当に泣きそうになりました...