2011-08-06 79 views
9

在我的iOS應用程序中,我嘗試使用CoreGraphics繪製曲線。繪圖本身工作正常,但在視網膜顯示器上,圖像使用相同的分辨率繪製,並且不會像素加倍。結果是一個像素化的圖像。使用CoreGraphics繪製視網膜顯示 - 圖像像素化

我使用以下功能圖紙:

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

我發現的建議是use the scaleFactor property,或CGContextSetShouldAntialias() function,但至今沒有這些幫助。 (雖然我可能使用不當。)

任何幫助將不勝感激。

回答

27

您需要

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

UIGraphicsBeginImageContextWithOptions在4.x版的軟件推出,取代UIGraphicsBeginImageContext。如果你打算在3.x設備上運行這個代碼,你需要弱化鏈接UIKit框架。如果您的部署目標是4.x或更高版本,則只需使用UIGraphicsBeginImageContextWithOptions即可,無需任何額外的檢查。

+0

它完美的作品!謝謝! – antalkerekes