Cut a UIImage into a circle

后端 未结 11 2261
面向向阳花
面向向阳花 2020-11-27 13:30

I want to cut a UIImage into a circle so that I can then use it as an annotation. Every answer on this site that I\'ve found describes creating an UIImage

相关标签:
11条回答
  • 2020-11-27 14:02

    You can use this code to circle Image

    extension UIImage {
    func circleImage(_ cornerRadius: CGFloat, size: CGSize) -> UIImage? {
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        if let context = UIGraphicsGetCurrentContext() {
            var path: UIBezierPath
            if size.height == size.width {
                if cornerRadius == size.width/2 {
                    path = UIBezierPath(arcCenter: CGPoint(x: size.width/2, y: size.height/2), radius: cornerRadius, startAngle: 0, endAngle: 2.0*CGFloat(Double.pi), clockwise: true)
                }else {
                    path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
                }
            }else {
                path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
            }
            context.addPath(path.cgPath)
            context.clip()
            self.draw(in: rect)
            // 从上下文上获取剪裁后的照片
            guard let uncompressedImage = UIGraphicsGetImageFromCurrentImageContext() else {
                UIGraphicsEndImageContext()
                return nil
            }
            // 关闭上下文
            UIGraphicsEndImageContext()
            return uncompressedImage
        }else {
            return nil
        }
    }}
    
    0 讨论(0)
  • Based on Nikos answer:

    public extension UIImage {
    
    func roundedImage() -> UIImage {
        let imageView: UIImageView = UIImageView(image: self)
        let layer = imageView.layer
        layer.masksToBounds = true
        layer.cornerRadius = imageView.frame.width / 2
        UIGraphicsBeginImageContext(imageView.bounds.size)
        layer.render(in: UIGraphicsGetCurrentContext()!)
        let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return roundedImage!
        }
    
    }
    
    //Usage
    
    let roundedImage = image.roundedImage()
    
    0 讨论(0)
  • 2020-11-27 14:05

    Xcode 8.1, Swift 3.0.1

    My code will look like this:

    let image = yourImage.resize(CGSize(width: 20, height: 20))?.circled(forRadius: 20)
    

    Add UIImage Extension, then:

    func resize(_ size: CGSize) -> UIImage? {
        let rect = CGRect(origin: .zero, size: size)
        return redraw(in: rect)
    }
    
    func redraw(in rect: CGRect) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
    
        guard let context = UIGraphicsGetCurrentContext(), let cgImage = cgImage else { return nil }
    
        let rect = CGRect(origin: .zero, size: size)
        let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: rect.size.height)
    
        context.concatenate(flipVertical)
        context.draw(cgImage, in: rect)
    
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    func circled(forRadius radius: CGFloat) -> UIImage? {
        let rediusSize = CGSize(width: radius, height: radius)
        let rect = CGRect(origin: .zero, size: size)
    
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
    
        guard let context = UIGraphicsGetCurrentContext(), let cgImage = cgImage else { return nil }
    
        let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: rect.size.height)
        context.concatenate(flipVertical)
    
        let bezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners: [.allCorners], cornerRadii: rediusSize)
        context.addPath(bezierPath.cgPath)
        context.clip()
    
        context.drawPath(using: .fillStroke)
        context.draw(cgImage, in: rect)
    
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        return image
    }
    
    0 讨论(0)
  • 2020-11-27 14:05

    swift 3 conform to MVC pattern create an external file

    @IBDesignable
    class RoundImage: UIImageView{
    
    
    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet{
            self.layer.cornerRadius = cornerRadius
        }
    }
    
    // set border width
    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet{
            self.layer.borderWidth = borderWidth
        }
    }
    
    // set border color
    
    @IBInspectable var borderColor: UIColor = UIColor.clear {
        didSet{
            self.layer.borderColor = borderColor.cgColor
        }
    }
    
    override func awakeFromNib() {
        self.clipsToBounds = true
    }
    
    
    }// class
    

    call class in the IB on storyboard

    set cornerradius as you please (1/2 of width if desire circle)

    Done!

    0 讨论(0)
  • 2020-11-27 14:08

    All these answers were really complex for a straight forward solution. I just replicated my Objective-C code and adjusted for Swift.

    self.myImageView?.layer.cornerRadius = (self.myImageView?.frame.size.width)! / 2;
    self.myImageView?.clipsToBounds = true
    
    0 讨论(0)
提交回复
热议问题