0

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

我的基本设置是,我有一个视图控制器,它拥有一个视图控制器,UIPageViewController我在其中加载具有用户手指可以绘制的视图的控制器。基本上我有一本可以画进去的书。

当书中翻页时,我通过调用保存图像

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

然后它的返回值被保存到一个文件中。但是,当我调用此保存方法时,则行

[self.layer renderInContext:UIGraphicsGetCurrentContext()];

被击中,这似乎调用了我的drawRect:方法,该方法被覆盖如下,

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

[self.currentImage drawAtPoint:CGPointMake(0.0f, 0.0f)];

我对造成这种情况的原因完全不知所措,并且一直让我发疯。

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

在此处输入图像描述

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

在此处输入图像描述

真的很感激任何人都可以给予的帮助和洞察力!!

编辑:(触摸移动添加)

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

}

4

1 回答 1

0

你不能打电话[self.layer renderInContext:]drawRect:,期间。如您所见,如果实现了renderInContext:调用drawRect:,则会导致无限递归。

您应该单独渲染一本书,然后使用渲染的图像让用户在上面绘图。UIGraphicsBeginImageContext()一种方法是在/UIGraphicsEndImageContext()块中包含一个独立的绘图代码,并在drawRect:. 另一种方法是使用两个不同的视图,它们不是彼此的子视图,一个绘制一本未改动的书,另一个绘制用户草图。

编辑:首先,您需要一个原始图像(书页,无论如何)。在不调用的情况下将其绘制在UIGraphicsBeginImageContext()/UIGraphicsEndImageContext()块中并将其保存在视图的 ivar 中(例如,)。在调用中(它会调用你)来更新视图。用作背景图像。 [self.layer renderInContext:]self.currentImagetouchesMoved:withEvent:setNeedsDisplaydrawRect:self.currentImagedrawRect:

我希望我不会太含糊。

于 2012-04-12T18:12:41.397 回答