2

サイズが約 3000x3000 の非常に大きな UIView があります。この大きなビューでは、iPad でスタイラスまたは指を使って自由な形の描画を行っています。これが私のタッチの開始メソッドと移動メソッドのコードです。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{    

        Line_Point * to_be_added = [[Line_Point alloc] init];
        to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
        to_be_added.line_pattern = [NSString stringWithString: current_line_pattern]; 

        UITouch *touch = [[event touchesForView:self] anyObject];

        CGPoint location = [touch locationInView:self];
        to_be_added.point = [NSValue valueWithCGPoint:(location)];  

        if (self.points == nil)
        {
            NSMutableArray *newPoints = [[NSMutableArray alloc] init];
            self.points = newPoints;
            [newPoints release];
        }

        [self.points addObject:to_be_added];           



}

これは、アプリがsetNeedsDisplayを呼び出すため、十分に呼び出されないため、問題を引き起こしているように見える私のタッチ移動メソッドです

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

        Line_Point * to_be_added = [[Line_Point alloc] init];
        to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
        to_be_added.line_pattern = [NSString stringWithString: current_line_pattern];

        UITouch * touch = [[event touchesForView:self] anyObject];
        CGPoint location = [touch locationInView:self];
        to_be_added.point = [NSValue valueWithCGPoint:(location)];

        [self.points addObject:to_be_added];           


        [self setNeedsLayout]; 

}

ここに、描画を表示する drawRect メソッドがあります

-(void)drawRect:(CGRect)rect
{
 if (self.points.count == 0)        
    {

    }
    else 
    {    
        Line_Point * first = [self.points objectAtIndex:0];
        CGContextRef context2 = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context2, first.line_color.CGColor);
        if ([first.line_pattern isEqualToString:@"normal"]) 
        {
            CGContextSetLineWidth(context2, 4.0);
            CGContextSetLineDash(context2, 0, NULL, 0);
        }
        else if([first.line_pattern isEqualToString:@"thick"])            
        {
            CGContextSetLineWidth(context2, 6.0);
            CGContextSetLineDash(context2, 0, NULL, 0);
        }
        else if([first.line_pattern isEqualToString:@"dotted"])            
        {
            CGFloat Pattern[] = {5, 5, 5, 5};
            CGContextSetLineDash(context2, 0, Pattern, 4);
        }
        else if([first.line_pattern isEqualToString:@"super_dotted"])            
        {
            CGFloat Pattern[] = {1, 2, 1, 2}; 
            CGContextSetLineDash(context2, 0, Pattern, 4); 
        }           
        CGPoint firstPoint2 = [first.point CGPointValue];
        CGContextBeginPath(context2);
        CGContextMoveToPoint(context2, firstPoint2.x, firstPoint2.y);

        int i2 = 1;
        while (i2 < self.points.count)
        {
            Line_Point * nextPoint = [self.points objectAtIndex:i2]; 

            if (nextPoint.point.CGPointValue.x < 0 && nextPoint.point.CGPointValue.y < 0)
            {
                CGContextDrawPath(context2, kCGPathStroke);

                if (i2 < (self.points.count-1))
                {

                    CGContextBeginPath(context2);
                    Line_Point * nextPoint2 = [self.points objectAtIndex:i2+1];                
                    CGContextMoveToPoint(context2, nextPoint2.point.CGPointValue.x, nextPoint2.point.CGPointValue.y);
                    CGContextSetStrokeColorWithColor(context2, nextPoint2.line_color.CGColor);
                    if ([nextPoint2.line_pattern isEqualToString:@"normal"]) 
                    {
                        CGContextSetLineWidth(context2, 4.0);
                        CGContextSetLineDash(context2, 0, NULL, 0);
                    }
                    else if([nextPoint2.line_pattern isEqualToString:@"thick"])            
                    {
                        CGContextSetLineWidth(context2, 6.0);
                        CGContextSetLineDash(context2, 0, NULL, 0);
                    }
                    else if([nextPoint2.line_pattern isEqualToString:@"dotted"])            
                    {
                        CGFloat Pattern[] = {5, 5, 5, 5};
                        CGContextSetLineDash(context2, 0, Pattern, 4);  
                    }
                    else if([nextPoint2.line_pattern isEqualToString:@"super_dotted"])            
                    {
                        CGFloat Pattern[] = {1, 2, 1, 2}; 
                        CGContextSetLineDash(context2, 0, Pattern, 4);
                    }        
                    i2 = i2 + 2;

                }
                else
                    i2++;
            }
            else
            {
                CGContextAddLineToPoint(context2, nextPoint.point.CGPointValue.x, nextPoint.point.CGPointValue.y);
                i2++;
            }
        }

        CGContextDrawPath(context2, kCGPathStroke);
    }

フリーフォーム モードで描画すると、アプリの動作が非常に遅くなります。ビューが大きすぎて、タッチ移動メソッドが呼び出されるたびに setNeedsDisplay がビュー全体をリロードしているためだと思います。setNeedsDisplay メソッドを高速化して、自由形式の描画が iPad で遅れないようにする方法はありますか。全体をリロードせずに、描画が行われているコンテキストをターゲットにすることはできますか? サイズを約 1000x1000 に調整すると、ラグの問題はありません。

この件についてご協力いただきありがとうございます。

-SetNeedsDisplayinRectを使用して編集を試みました

UIView は UISCrollview 内にラップされており、コードは次のとおりです。

   -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
   {

    Line_Point * to_be_added = [[Line_Point alloc] init];
    to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
    to_be_added.line_pattern = [NSString stringWithString: current_line_pattern];

    UITouch * touch = [[event touchesForView:self] anyObject];
    CGPoint location = [touch locationInView:self];
    to_be_added.point = [NSValue valueWithCGPoint:(location)];

    [self.points addObject:to_be_added];           


          CGRect visibleRect;
        visibleRect.origin = big_view.scrollable_view.contentOffset;
        visibleRect.size = CGSizeMake(950, 500);            
        [self setNeedsDisplayInRect:visibleRect];  

 }

このアプローチ方法も役に立ちません。

あなたが助けてくれるなら、もう一度感謝します。

4

2 に答える 2

2

あなたが使用することができます

- (void)setNeedsDisplayInRect:(CGRect)invalidRect

-setNeedsDisplay の代わりに

-drawInRect が呼び出されるたびにすべてのポイントをレンダリングしないという別の解決策 (ポイントが多すぎて、それらを描画するためにアプリが遅れる場合)。たとえば、10回目の呼び出しごとに現在のコンテキストから画像を保存し、画像のみを表示できます。

アップデート:

追加してみてください

+(Class)layerClass
{
return [CATiledLayer class];
}

scrollView のビューに

于 2012-06-20T22:06:08.627 に答える
0

明確にするために、3000x3000ピクセルのUIViewには、3000x3000x4バイトのスペースを使用するCALayerが背後にあります(タイル化されているかどうかはそれほど重要ではありません)。つまり、約34〜35MBです。踏み台に割り当てられているため、アプリ自体にそのメモリ使用量は表示されません。

私の観点からの最善の解決策は、ビューよりも少し大きいレイヤーを持つタイルレイヤーを作成し(スレッドからレンダリングできるため)、クリッピングを有効にして、適切なオフセットでパスをレイヤーに割り当てることです。 。それはかなりの作業ですが、かなりうまくいくでしょう。

ああ、時々、再描画を行うと、CALayersキャッシュをクリアするのに役立ちます。layer.contents=nilはそれを行います...。

乾杯、イェルク

于 2012-06-20T22:38:04.340 に答える