2013-11-21 177 views
29

我嘗試用簡單的藍色圓圈製作一個20x20的UIImage。 我嘗試使用此功能,但結果是黑色方塊中的藍色圓圈。 如何去除圓周的黑色方塊?繪製一個簡單的圓圈uiimage

功能:

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

+1

潛在的,你應該使用視圖,而不是一個ImageView的http://stackoverflow.com/questions/6589265/how-to-繪製一個自定義uiview,這只是一個圈子iphone應用程序 – Woodstock

回答

29

您需要包括alpha通道成位圖:如果你想看到什麼角落背後UIGraphicsBeginImageContextWithOptions(..., NO, ...)

27

感謝您的問答& A! 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 
    } 
} 

迅速版本3由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

Swift 3代碼下面爲他人。順便謝謝你的解決方案。 – Schemetrical

5

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

迅速自動更正敬虔。感謝瓦倫丁。

2

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

試試這個

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