似乎在尝试将我的观点纳入上下文时遇到了一些问题。
我的基本设置是,我有一个视图控制器,它拥有一个视图控制器,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];
}