2013-11-21 30 views
29

Basit bir mavi daire ile 20x20 UIImage yapmaya çalışıyorum. Bu işlevle deniyorum, ancak sonuç, siyah bir kare içinde mavi bir dairedir. Dairenin çevresindeki siyah kareyi nasıl kaldırırım?Basit bir daire çizme uiimage

Fonksiyon:

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

gerçek sonuç: enter image description here

+1

Potansiyel Eğer bir imageView http://stackoverflow.com/questions/6589265/how-to- yerine bir görünüm kullanmanız gerekir deneyin bir-çizmek-bir-uiview-bu-sadece-bir-daire-iphone-app – Woodstock

cevap

29
Sen bitmap halinde alfa kanalı dahil etmek gerek

: UIGraphicsBeginImageContextWithOptions(..., NO, ...) köşelerde arkasında ne olduğunu görmek istiyorum.

27

Q & A için teşekkürler! aşağıdaki gibi Swift kodu: Schemetrical tarafından sağlanan

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 versiyonu:

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

diğerleri için Swift 3 kodu. Bu arada çözüm için teşekkürler. – Schemetrical

5

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 otomatik düzeltme godly olup. Valentin'e teşekkürler.

2

Xamarin.iOS örnek:

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

bu

UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);