gpt4 book ai didi

ios - 画一个简单的圆uiimage

转载 作者:IT王子 更新时间:2023-10-29 07:41:58 26 4
gpt4 key购买 nike

我尝试制作一个带有简单蓝色圆圈的 20x20 UIImage。我尝试使用此功能,但结果是黑色方 block 中的蓝色圆圈。如何去除圆圈周围的黑色方 block ?

功能:

+ (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;
}

实际结果: enter image description here

最佳答案

感谢您的问答! 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
}
}

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
}
}

关于ios - 画一个简单的圆uiimage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20116472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com