2011-08-08 8 views
0

私はUIImageを持っています。私はそれにユーザの指に続く線を引いています。それは抽選盤のようなものです。これは、UIImageが小さく、例えば500 x 600の場合は完全に機能しますが、1600 x 1200のようなものであれば、それは本当にかすれて遅くなります。私はこれを最適化する方法はありますか?これはtouchesModeの私の描画コードです:大きなUIImageのCoreGraphics図面

UIGraphicsBeginImageContext(self.frame.size); 
[drawImageView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
//CGContextSetAlpha(UIGraphicsGetCurrentContext(), 0.5f); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

ありがとうございました。

答えて

0

1600X1200フレーム全体を一度に描画するのではなく、必要に応じて描画してみましょう。私が意味することは、あなたが不気味なパフォーマンスを持っているフレーム全体(メモリ内にある)を描画しているからです。

お試しCATiledLayer基本的には、あなたのデバイスが可能な画面サイズだけを描く必要があります。ユーザーがスクロールするときに、それをオンザフライで描く必要があります。

これはiPadまたはiPhoneのGoogleマップで使用されているものです。これが役に立ったら...

0

タッチが動くたびに新しいコンテキストを作成し、現在のイメージを描画する代わりに、CGBitmapContextCreateを使用してコンテキストを作成し、そのコンテキストを再利用してください。以前のすべての描画はすでにコンテキスト内にあるため、タッチが動くたびに新しいコンテキストを作成する必要はありません。

- (CGContextRef)drawingContext { 
    if(!context) { // context is an instance variable of type CGContextRef 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
     if(!colorSpace) return nil; 
     context = CGBitmapContextCreate(NULL, contextSize.width, contextSize.height, 
             8, contextSize.width * 32, colorSpace, 
             kCGImageAlphaPremultipliedFirst); 
     CGColorSpaceRelease(colorSpace); 
     if(!context) return nil; 
     CGContextConcatCTM(context, CGAffineTransformMake(1,0,0,-1,0,contextSize.height)); 
     CGContextDrawImage(context, (CGRect){CGPointZero,contextSize}, drawImageView.image.CGImage); 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
    } 
    return context; 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGContextRef ctxt = [self drawingContext]; 
    CGContextBeginPath(ctxt); 
    CGContextMoveToPoint(ctxt, lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(ctxt, currentPoint.x, currentPoint.y); 
    CGContextStrokePath(ctxt); 
    CGImageRef img = CGBitmapContextCreateImage(ctxt); 
    drawImageView.image = [UIImage imageWithCGImage:img]; 
    CGImageRelease(img); 
} 

このコードでは、インスタンス変数CGContextRef contextCGSize contextSizeが必要です。コンテキストはdeallocで解放する必要があります。また、そのサイズを変更するときはいつでも。