我可以在drawRect方法之外绘制圆形,矩形,直线等形状

Pra*_*kam 4 drawrect ipad ios cgcontextref

我可以drawRect使用外部方法绘制圆形,矩形,线条等形状

CGContextRef contextRef = UIGraphicsGetCurrentContext();
Run Code Online (Sandbox Code Playgroud)

或者drawRect仅在内部使用它是强制性的.请帮帮我,让我知道如何在drawRect方法外绘制形状.实际上我想继续在touchesMoved事件上绘制点.

这是我绘制点的代码.

CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 0, 255, 0, 1);
CGContextFillEllipseInRect(contextRef, CGRectMake(theMovedPoint.x, theMovedPoint.y, 8, 8));
Run Code Online (Sandbox Code Playgroud)

Vig*_*esh 15

基本上你需要一个上下文来绘制一些东西.您可以将上下文视为白皮书.如果你不在有效的上下文中,UIGraphicsGetCurrentContext它将返回.nulldrawRect你获得视图的上下文.

话虽如此,你可以在外面绘制drawRect方法.您可以开始使用imageContext绘制内容并将其添加到视图中.

看取自下面的例子在这里,

    - (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image
{
    // begin a graphics context of sufficient size
    UIGraphicsBeginImageContext(image.size);

    // draw original image into the context
    [image drawAtPoint:CGPointZero];

    // get the context for CoreGraphics
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // set stroking color and draw circle
    [[UIColor redColor] setStroke];

    // make circle rect 5 px from border
    CGRect circleRect = CGRectMake(0, 0,
                image.size.width,
                image.size.height);
    circleRect = CGRectInset(circleRect, 5, 5);

    // draw circle
    CGContextStrokeEllipseInRect(ctx, circleRect);

    // make image out of bitmap context
    UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();

    // free the context
    UIGraphicsEndImageContext();

    return retImage;
} 
Run Code Online (Sandbox Code Playgroud)