首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >drawRect:/renderInContext:问题

drawRect:/renderInContext:问题
EN

Stack Overflow用户
提问于 2012-04-13 01:58:16
回答 1查看 3.7K关注 0票数 0

似乎在尝试将我的观点引入上下文时遇到了一些问题。

我的基本设置是,我有一个拥有UIPageViewController的视图控制器,在这个控制器中,我加载了可以由用户手指绘制的视图控制器。基本上,我有一本可以被吸引的书。

在书中翻页时,我通过调用

代码语言:javascript
复制
- (UIImage *)wholeImage {
    // Render the layer into an image and return
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *wholePageImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return wholePageImage;
}

然后将这个的返回值保存到一个文件中。但是,当我调用这个save方法时,然后行

代码语言:javascript
复制
[self.layer renderInContext:UIGraphicsGetCurrentContext()];

,它似乎调用了我的drawRect:方法,该方法被重写如下,

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect {
    // Draw the current state of the image
    [self.currentImage drawAtPoint:CGPointMake(0.0f, 0.0f)];
    CGPoint midPoint1 = [self midPointOfPoint1:previousPoint1 point2:previousPoint2];
    CGPoint midPoint2 = [self midPointOfPoint1:currentPoint point2:previousPoint1];

    // Get the context
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Add the new bit of line to the image
    [self.layer renderInContext:context];
    CGContextMoveToPoint(context, midPoint1.x, midPoint1.y);
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, midPoint2.x, midPoint2.y); 
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);
    if (self.drawMode == LNDrawViewDrawModeTipex) CGContextSetBlendMode(context, kCGBlendModeClear);
    else CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGContextSetStrokeColorWithColor(context, self.lineColour.CGColor);
    CGContextStrokePath(context);

    // Call super
    [super drawRect:rect];
}

这是有道理的,但它似乎是递归调用的,直到最终我在这一行上得到一个EXC_BAD_ACCESS

代码语言:javascript
复制
[self.currentImage drawAtPoint:CGPointMake(0.0f, 0.0f)];

我完全不知道是什么导致了这一点,并一直让我发疯。

如果有帮助,我的调用堆栈是这样开始的

,然后递归地继续,直到它最终以

我真的很感激,任何人都能给予的帮助和洞察力!

编辑:(添加了已移动的触点)

代码语言:javascript
复制
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // Get the touch
    UITouch *touch = [touches anyObject];

    // Get the points
    previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

    // Calculate mid point
    CGPoint mid1 = [self midPointOfPoint1:previousPoint1 point2:previousPoint2]; 
    CGPoint mid2 = [self midPointOfPoint1:currentPoint point2:previousPoint1];

    // Create a path for the last few points
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
    CGPathAddQuadCurveToPoint(path, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGRect pathBounds = CGPathGetBoundingBox(path);
    CGPathRelease(path);

    // Take account of line width
    pathBounds.origin.x -= self.lineWidth * 2.0f;
    pathBounds.origin.y -= self.lineWidth * 2.0f;
    pathBounds.size.width += self.lineWidth * 4.0f;
    pathBounds.size.height += self.lineWidth * 4.0f;

    UIGraphicsBeginImageContext(pathBounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    self.currentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self setNeedsDisplayInRect:pathBounds];    

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-13 02:12:41

不能在drawRect:中调用[self.layer renderInContext:],句号。正如您已经了解到的,如果实现了drawRect:renderInContext:将调用它,这将导致无限递归。

您应该单独呈现一本书,然后使用呈现的图像让用户在其上绘制。要做到这一点,一种方法是在UIGraphicsBeginImageContext()/UIGraphicsEndImageContext()块中包含一个自包含的绘图代码,并在drawRect:中使用结果图像。另一种方法是使用两个不同的视图,它们不是彼此的子视图,一个绘制未更改的书,另一个绘制用户草图。

编辑:首先,你需要一个原始图片(书页,随便什么)。在不调用[self.layer renderInContext:]的情况下将其绘制在UIGraphicsBeginImageContext()/UIGraphicsEndImageContext()块中,并将其保存在视图的ivar中(比如self.currentImage)。在touchesMoved:withEvent:中,调用setNeedsDisplay (它将为您调用drawRect: )来更新视图。使用drawRect:中的self.currentImage作为背景图像。

我希望我没有说得太含糊。

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

https://stackoverflow.com/questions/10129106

复制
相关文章

相似问题

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