首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CgLayer进行优化绘图

如何使用CgLayer进行优化绘图
EN

Stack Overflow用户
提问于 2012-07-05 17:46:08
回答 1查看 3K关注 0票数 0

我已经创建了一个简单的绘图项目,代码运行良好,但我想将绘图缓存到CGlayer中,因为我读到这是一种更有效的绘图方式。我已经通读了这些文件,但不能正确地理解它。所以朋友们,我请求你们在这方面帮助我。

下面是我的代码,我想知道如何在这里使用CgLayer

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect
{

   CGContextRef context = UIGraphicsGetCurrentContext();

   if(myLayerRef == nil)
   {

       myLayerRef = CGLayerCreateWithContext(context, self.bounds.size, NULL);
   }

    CGContextRef layerContext = CGLayerGetContext(myLayerRef);

    CGContextDrawLayerAtPoint(context, CGPointZero, myLayerRef);   
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];

    m_previousPoint2 = m_previousPoint1;
    m_previousPoint1 = [mytouch previousLocationInView:self];
    m_currentPoint = [mytouch locationInView:self];

    CGPoint mid1    = midPoint(m_previousPoint1, m_previousPoint2); 
    CGPoint mid2    = midPoint(m_currentPoint, m_previousPoint1);  

    testpath = CGPathCreateMutable();
    CGPathMoveToPoint(testpath, NULL, mid1.x, mid1.y);

    CGPathAddQuadCurveToPoint(testpath, NULL, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);       


    CGContextRef context = UIGraphicsGetCurrentContext();

    context = CGLayerGetContext(myLayerRef);

    CGRect bounds = CGPathGetBoundingBox(testpath);   

     CGPathRelease(testpath);


    CGRect drawBox = bounds;

    //Pad our values so the bounding box respects our line width
    drawBox.origin.x        -= self.lineWidth * 2;
    drawBox.origin.y        -= self.lineWidth * 2;
    drawBox.size.width      += self.lineWidth * 4;
    drawBox.size.height     += self.lineWidth * 4;


   [self setNeedsDisplayInRect:drawBox];       
}


- (void) drawingOperations
{

    CGContextRef context1 = CGLayerGetContext(myLayerRef);



    CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
    CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);



    CGContextMoveToPoint(context1, mid1.x, mid1.y);
    CGContextAddQuadCurveToPoint(context1, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
    CGContextSetLineCap(context1, kCGLineCapRound);
    CGContextSetLineWidth(context1, self.lineWidth);
    CGContextSetStrokeColorWithColor(context1, self.lineColor.CGColor);

    CGContextSetFlatness(context1, 2.0);

    CGContextSetAllowsAntialiasing(context1, true);


    CGContextStrokePath(context1);
}

关于Ranjit

EN

回答 1

Stack Overflow用户

发布于 2014-08-14 16:34:37

不再是使用CGLayer的最佳选择

http://iosptl.com/posts/cglayer-no-longer-recommended/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11341763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档