drawRect: / renderInContext: Probleme

Es scheint ein Problem zu sein, meine Sichtweise in einen Kontext zu bringen.

Meine Grundeinstellung ist, ich habe einen View-Controller, der eine besitztUIPageViewController in dem ich Steuerungen mit Ansichten lade, die vom Benutzerfinger gezeichnet werden können. Grundsätzlich habe ich ein Buch, das sich einzeichnen lässt.

Wenn die Seite im Buch umblättert wird, speichere ich das Bild durch einen Anruf

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

Der Rückgabewert davon wird dann in einer Datei gespeichert. Allerdings, wenn ich diese Speichermethode dann Zeile aufrufen

<code>[self.layer renderInContext:UIGraphicsGetCurrentContext()];
</code>

wird getroffen, was einen Anruf bei mir zu machen scheintdrawRect: Methode, die wie folgt überschrieben wird,

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

Das macht Sinn, aber es scheint rekursiv aufgerufen zu werden, bis ich irgendwann einen EXC_BAD_ACCESS in dieser Zeile erhalte

<code>[self.currentImage drawAtPoint:CGPointMake(0.0f, 0.0f)];
</code>

Bin völlig ratlos, was dies verursacht und hat mich verrückt gemacht.

Wenn es hilft, beginnt mein Call-Stack so

und dann rekursiv weiter, bis es schließlich mit endet

Würde mich sehr freuen und helfen und Einblicke geben, die jeder geben kann !!

BEARBEITEN: (Berührungen verschoben hinzugefügt)

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

}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage