似乎在尝试将我的视图绘制到上下文中时遇到了一些问题。
我的基本设置是,我有一个拥有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];
}
答案 0 :(得分:0)
您不得在[self.layer renderInContext:]
期间致电drawRect:
。如您所知,renderInContext:
调用drawRect:
(如果已实现),这将导致无限递归。
您应该单独渲染一本书,然后使用渲染图像让用户在其上绘制。一种方法是在UIGraphicsBeginImageContext()
/ UIGraphicsEndImageContext()
块中包含一个自包含的绘图代码,并使用drawRect:
中生成的图像。另一种方法是使用两个不同的视图,这两个视图不是彼此的子视图,一个绘制一个未经改动的书,另一个绘制用户草图。
编辑:首先,您需要一张原始图片(书页,无论如何)。在UIGraphicsBeginImageContext()
/ UIGraphicsEndImageContext()
块中绘制它而不调用 [self.layer renderInContext:]
并将其保存在视图的ivar中(例如,self.currentImage
) 。在touchesMoved:withEvent:
调用setNeedsDisplay
(它会为您调用drawRect:
)来更新视图。使用self.currentImage
中的drawRect:
作为背景图片。
我希望我不要太模糊。