drawRect: / renderInContext: problemas
Parece estar tendo um pouco de um problema tentando desenhar minha visão em um contexto.
Minha configuração básica é, eu tenho um controlador de visão que possui umUIPageViewController
em que eu carrego controladores com vistas que podem ser desenhadas pelo dedo do usuário. Basicamente eu tenho um livro que pode ser desenhado.
Quando a página é virada no livro eu salvar a imagem chamando
<code>- (UIImage *)wholeImage { // Render the layer into an image and return UIGraphicsBeginImageContext(self.bounds.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *wholePageImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return wholePageImage; } </code>
O valor de retorno disso é salvo em um arquivo. No entanto, quando eu chamo este método salvar, em seguida, linha
<code>[self.layer renderInContext:UIGraphicsGetCurrentContext()]; </code>
é atingido, o que parece fazer uma ligação para o meudrawRect:
método, que é substituído da seguinte forma,
<code>- (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]; } </code>
o que faz sentido, mas parece ser chamado de forma recursiva até que eventualmente eu recebo um EXC_BAD_ACCESS nesta linha
<code>[self.currentImage drawAtPoint:CGPointMake(0.0f, 0.0f)]; </code>
Estou em uma perda completa sobre o que está causando isso e tem me deixando louco.
Se isso ajudar, minha pilha de chamadas começa assim
e, em seguida, continua de forma recursiva até que finalmente termina com
Realmente aprecio e ajuda e insight que qualquer um pode dar !!
EDIT: (toques movidos adicionados)
<code>-(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]; </code>
}