Draw a simple circle uiimage

前端 未结 6 1489
遥遥无期
遥遥无期 2020-12-28 12:33

I try to make a 20x20 UIImage with a simple blue circle. I try with this function, but the result is a blue circle in a black square. How do I remove the black square around

相关标签:
6条回答
  • 2020-12-28 12:48

    Thanks for the Q&A! Swift code as below:

    extension UIImage {
        class func circle(diameter: CGFloat, color: UIColor) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(diameter, diameter), false, 0)
            let ctx = UIGraphicsGetCurrentContext()
            CGContextSaveGState(ctx)
    
            let rect = CGRectMake(0, 0, diameter, diameter)
            CGContextSetFillColorWithColor(ctx, color.CGColor)
            CGContextFillEllipseInRect(ctx, rect)
    
            CGContextRestoreGState(ctx)
            let img = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return img
        }
    }
    

    Swift 3 version provided by Schemetrical:

    extension UIImage {
        class func circle(diameter: CGFloat, color: UIColor) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, 0)
            let ctx = UIGraphicsGetCurrentContext()!
            ctx.saveGState()
    
            let rect = CGRect(x: 0, y: 0, width: diameter, height: diameter)
            ctx.setFillColor(color.cgColor)
            ctx.fillEllipse(in: rect)
    
            ctx.restoreGState()
            let img = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
    
            return img
        }
    }
    
    0 讨论(0)
  • 2020-12-28 12:59

    A custom initializer for creating a circle with a specific diameter and color:

    extension UIImage {
        convenience init?(diameter: CGFloat, color: UIColor) {
            let size = CGSize(width: diameter, height: diameter)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            guard let contex = UIGraphicsGetCurrentContext() else {
                return nil
            }
    
            contex.saveGState()
            let rect = CGRect(origin: .zero, size: size)
            contex.setFillColor(color.cgColor)
            contex.fillEllipse(in: rect)
            contex.restoreGState()
    
            guard let image = UIGraphicsGetImageFromCurrentImageContext(),
            let cgImage = image.cgImage else {
                return nil
            }
            UIGraphicsEndImageContext()
    
            self.init(cgImage: cgImage)
        }
    }
    
    0 讨论(0)
  • 2020-12-28 13:03

    Xamarin.iOS sample:

        public static UIImage CircleImage(nfloat diameter, UIColor color, bool opaque = false)
        {
            var rect = new CGRect(0, 0, diameter, diameter);
    
            UIGraphics.BeginImageContextWithOptions(rect.Size, opaque, 0);
            var ctx = UIGraphics.GetCurrentContext();
            ctx.SaveState();
    
            ctx.SetFillColor(color.CGColor);
            ctx.FillEllipseInRect(rect);
    
            ctx.RestoreState();
            var img = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
    
            return img;
        }
    
    0 讨论(0)
  • 2020-12-28 13:04

    Try this

    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
    
    0 讨论(0)
  • 2020-12-28 13:06

    Swift 3 & 4:

    extension UIImage {
        class func circle(diameter: CGFloat, color: UIColor) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, 0)
            let ctx = UIGraphicsGetCurrentContext()!
            ctx.saveGState()
    
            let rect = CGRect(x: 0, y: 0, width: diameter, height: diameter)
            ctx.setFillColor(color.cgColor)
            ctx.fillEllipse(in: rect)
    
            ctx.restoreGState()
            let img = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
    
            return img
        }
    }
    

    Swift autocorrect is godly. Thanks to Valentin.

    0 讨论(0)
  • 2020-12-28 13:13

    You need to include alpha channel into your bitmap: UIGraphicsBeginImageContextWithOptions(..., NO, ...) if you want to see what is behind the corners.

    0 讨论(0)
提交回复
热议问题