2013-11-21 15 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- के बजाय एक दृश्य का उपयोग करना चाहिए की कोशिश करो ड्रॉ-ए-कस्टम-यूवीव-वह-बस-एक-सर्कल-आईफोन-ऐप – Woodstock

उत्तर

29

आप अपने बिटमैप में अल्फ़ा चैनल शामिल करने की जरूरत: UIGraphicsBeginImageContextWithOptions(..., NO, ...) अगर आप क्या कोनों के पीछे है देखना चाहता हूँ।

27

प्रश्न & ए के लिए धन्यवाद! नीचे के रूप में स्विफ्ट कोड:

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

दूसरों के लिए नीचे स्विफ्ट 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);