2011-08-06 14 views
9

En mi aplicación iOS, estoy tratando de dibujar curvas usando CoreGraphics. El dibujo en sí funciona bien, pero en la pantalla retina la imagen se dibuja con la misma resolución y no se duplica el píxel. El resultado es una imagen pixelada.Dibujar en la pantalla retina usando CoreGraphics - Imagen pixelada

Estoy dibujando mediante las siguientes funciones:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.canvasView]; 

    UIGraphicsBeginImageContext(self.canvasView.frame.size); 
    [canvasView.image drawInRect:self.canvasView.frame]; 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetShouldAntialias(ctx, YES); 
    CGContextSetLineCap(ctx, kCGLineCapRound); 
    CGContextSetLineWidth(ctx, 5.0); 
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(ctx, currentPoint.x, currentPoint.y); 
    CGContextStrokePath(ctx); 
    canvasView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
    // some code omitted from this example 
} 

El consejo que he encontrado era use the scaleFactor property, o la CGContextSetShouldAntialias() function, pero ninguno de estos ayudado hasta ahora. (Aunque podría haberlos utilizado incorrectamente.)

Cualquier ayuda sería muy apreciada.

Respuesta

27

Es necesario sustituir UIGraphicsBeginImageContext con

if (UIGraphicsBeginImageContextWithOptions != NULL) { 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 
} else { 
    UIGraphicsBeginImageContext(size); 
} 

UIGraphicsBeginImageContextWithOptions se introdujo en el software 4.x. Si va a ejecutar este código en dispositivos 3.x, necesita vincular débilmente el marco UIKit. Si su destino de despliegue es 4.xo superior, puede usar UIGraphicsBeginImageContextWithOptions sin ninguna verificación adicional.

+0

¡Funciona a la perfección! ¡Gracias! – antalkerekes

Cuestiones relacionadas