【问题标题】:How to draw a full circle with image如何用图像画一个完整的圆
【发布时间】:2013-04-23 09:33:54
【问题描述】:

我对核心图形很陌生。我需要画一个完整的圆圈。我该怎么做?我目前有一个蒙面圈。

我提到了this example

//Use the draw rect to draw the Background, the Circle and the Handle 
-(void)drawRect:(CGRect)rect{

    [super drawRect:rect];

    CGContextRef ctx = UIGraphicsGetCurrentContext();

/** Draw the Background **/

    //Create the path
    CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, radius, 0, M_PI *2, 0);

//    CGContextFillEllipseInRect(ctx, CGRectMake(self.frame.size.height/2, self.frame.size.width/2, 100, 100));
//    CGContextSetRGBFillColor(ctx, 0.5, 0.5, 0.5, 1.0);


    //Set the stroke color to black
    [[UIColor greenColor]setStroke];

    //Define line width and cap
    CGContextSetLineWidth(ctx, TB_BACKGROUND_WIDTH);
    CGContextSetLineCap(ctx, kCGLineCapButt);

    CGContextDrawPath(ctx, kCGPathStroke);

    UIGraphicsBeginImageContext(CGSizeMake(TB_SLIDER_SIZE,TB_SLIDER_SIZE));
    CGContextRef imageCtx = UIGraphicsGetCurrentContext();

    CGContextAddArc(imageCtx, self.frame.size.width/2  , self.frame.size.height/2, radius, 0, ToRad(self.self.startAngle), 0);

    [[UIColor redColor]set];

    CGContextSetLineWidth(imageCtx, TB_LINE_WIDTH);
    CGContextDrawPath(imageCtx, kCGPathStroke);

    //save the context content into the image mask
    CGImageRef mask = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
    UIGraphicsEndImageContext();
    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, self.bounds, mask);
    CGImageRelease(mask);

    //list of components
    CGFloat components[8] = {
        0.0, 0.0, 1.0, 1.0,     // Start color - Blue
        1.0, 1.0, 1.0, 1.0 };   // End color - Violet

    CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, components, NULL, 2);
    CGColorSpaceRelease(baseSpace), baseSpace = NULL;

    //Gradient direction
    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

    //Draw the gradient
    CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
    CGGradientRelease(gradient), gradient = NULL;

    CGContextRestoreGState(ctx);

/** Draw the handle **/
    [self drawTheHandle:ctx];

}

【问题讨论】:

  • 完整的圆圈是什么意思?你想创建一个简单的圆圈填充一些颜色吗?
  • 嗨@amit3117,我想要一个完全填充的圆圈而不是中间的黑色整体。

标签: iphone ios xcode core-graphics geometry


【解决方案1】:
CGContextDrawPath(imageCtx, kCGPathFill);

【讨论】:

  • 谢谢你能告诉我如何使内圆变小吗?
  • 我认为你得到圆圈的原因是因为你将线宽设置为 TB_BACKGROUND_WIDTH。如果您希望孔更小,则将线宽设置得更大。这也会使外部半径更大,因此您可能需要减小 CGContextAddArc 半径。
【解决方案2】:

将backgroup宽度和线宽设置为140就可以了!!

#define TB_BACKGROUND_WIDTH 140    //The width of the dark background
#define TB_LINE_WIDTH 140          //The width of the active area (the gradient) and the width of the handle

【讨论】:

    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    相关资源
    最近更新 更多