iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 我可以在drawRect方法之外绘制圆形,矩形,直线等形状大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以在drawRect方法之外使用绘制圆形,矩形,直线等形状

CGContextRef contextRef = UIGraphicsGetCurrentContext();

或者仅在drawRect中使用它是必须的.
请帮帮我,让我知道如何在drawRect方法之外绘制形状.
其实我想继续在touchesMoved事件上绘制点.

这是我绘制点的代码.

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

解决方法

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

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

请看下面的例子,取自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,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;
}

大佬总结

以上是大佬教程为你收集整理的ios – 我可以在drawRect方法之外绘制圆形,矩形,直线等形状全部内容,希望文章能够帮你解决ios – 我可以在drawRect方法之外绘制圆形,矩形,直线等形状所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。