Ich versuche, eine 20x20-UIImage mit einem einfachen blauen Kreis zu erstellen ... Ich versuche es mit dieser Funktion, aber das Ergebnis ist ein blauer Kreis in einem schwarzen Quadrat .. Wie entferne ich das schwarze Quadrat um den Kreis?
Funktion:
+ (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;
}
tatsächliche Ergebnis:
Sie müssen den Alphakanal in Ihre Bitmap aufnehmen: UIGraphicsBeginImageContextWithOptions(..., NO, ...)
, wenn Sie sehen möchten, was sich hinter den Ecken befindet.
Vielen Dank für das Q & A! Swift-Code wie folgt:
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 von 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
}
}
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
}
}
Schnelle Autokorrektur ist göttlich. Danke an Valentin.
Xamarin.iOS-Beispiel:
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;
}
Versuche dies
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
Ein benutzerdefinierter Initialisierer zum Erstellen eines Kreises mit einem bestimmten Durchmesser und einer bestimmten Farbe:
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)
}
}