Can I draw shapes like circle,rectangle,line etc outside drawRect method

北慕城南 提交于 2019-12-22 09:05:33

问题


Can I draw shapes like circle, rectangle, line etc outside drawRect method using

CGContextRef contextRef = UIGraphicsGetCurrentContext();

or is it mandatory to use it inside drawRect only. Please help me, let me know how can I draw shapes outside drawRect method. Actually i want to go on plotting dots on touchesMoved event.

This is my code for drawing a dot.

CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 0, 255, 0, 1);
CGContextFillEllipseInRect(contextRef, CGRectMake(theMovedPoint.x, theMovedPoint.y, 8, 8));

回答1:


Basically you need a context to draw something. You can assume context as a white paper. UIGraphicsGetCurrentContext will return null if you are not in a valid context.In drawRect you get the context of the view.

Having said that, you can draw outside drawRect Method. You can begin an imageContext to draw things and add it to your view.

Look at the below example taken from here,

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



回答2:


For Swift 4

func imageByDrawingCircle(on image: UIImage) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(CGSize(width: image.size.width, height: image.size.height), false, 0.0)

    // draw original image into the context
    image.draw(at: CGPoint.zero)

    // get the context for CoreGraphics
    let ctx = UIGraphicsGetCurrentContext()!

    // set stroking color and draw circle
    ctx.setStrokeColor(UIColor.red.cgColor)

    // make circle rect 5 px from border
    var circleRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
    circleRect = circleRect.insetBy(dx: 5, dy: 5)

    // draw circle
    ctx.strokeEllipse(in: circleRect)

    // make image out of bitmap context
    let retImage = UIGraphicsGetImageFromCurrentImageContext()!

    // free the context
    UIGraphicsEndImageContext()

    return retImage;
}


来源:https://stackoverflow.com/questions/16813090/can-i-draw-shapes-like-circle-rectangle-line-etc-outside-drawrect-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!