ios 画一个简单的圆 uiimage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20116472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw a simple circle uiimage
提问by Mattia Lancieri
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 the circle?
我尝试用一个简单的蓝色圆圈制作一个 20x20 的 UIImage。我尝试使用此功能,但结果是黑色方块中的蓝色圆圈。如何去除圆圈周围的黑色方块?
Function:
功能:
+ (UIImage *)blueCircle {
static UIImage *blueCircle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 20.f), YES, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGRect rect = CGRectMake(0, 0, 20, 20);
CGContextSetFillColorWithColor(ctx, [UIColor cyanColor].CGColor);
CGContextFillEllipseInRect(ctx, rect);
CGContextRestoreGState(ctx);
blueCircle = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});
return blueCircle;
}
actual result:
实际结果:
采纳答案by yurish
You need to include alpha channel into your bitmap: UIGraphicsBeginImageContextWithOptions(..., NO, ...)
if you want to see what is behind the corners.
您需要在位图中包含 alpha 通道:UIGraphicsBeginImageContextWithOptions(..., NO, ...)
如果您想查看角落后面的内容。
回答by superarts.org
Thanks for the Q&A! Swift code as below:
感谢您的问答!Swift 代码如下:
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:
Schemetrical 提供的 Swift 3 版本:
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
}
}
回答by Schemetrical
Swift 3 & 4:
斯威夫特 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.
Swift 自动更正是神圣的。感谢瓦伦丁。
回答by Jelle
Xamarin.iOS sample:
Xamarin.iOS 示例:
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;
}
回答by Amr Angry
Try this
尝试这个
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
回答by CodeBender
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)
}
}