4

これが私の最初の質問ですので、ご容赦ください。

基本的に簡単な描画アプリを作成しようとしていますが、以前はCore Graphicsを使用していましたが、問題は遅すぎたため、指で描画すると遅れてしまいました。

ですから、私はUIBezierパスを使用して描画しようとしています。これは、はるかに高速であることがわかったためです。

Core Graphicsを使用していたとき、描画速度を維持するために、作成したカスタムビットマップコンテキストに描画していました。このコンテキストは、描画中に常に更新されていました。

そこで、カスタムビットマップコンテキストに描画し、次にCGImageRefを-を使用してそのコンテキストで描画されたものに設定しました。

cacheImage = CGBitmapContextCreateImage(imageContext); 

そして、それは-を使用してビットマップコンテキストに引き戻されました

CGContextDrawImage(imageContext, self.bounds, cacheImage); )

また、これを行ったので、描画する線の色を変更しても、それが理にかなっている場合は、残りの描画は以前に描画したままになりました。

今私が遭遇した問題はこれです。

-を使用して、画像コンテキストへのUIBezierパスを描画しようとしています。

imageContext = UIGraphicsGetCurrentContext();

UIGraphicsPushContext(imageContext);


[path stroke];


if(imageContext != nil){
    cacheImage = CGBitmapContextCreateImage(imageContext);  //invalid context here so added to solve

}


CGContextScaleCTM(imageContext, 1, -1); // using this as UIBezier 

CGContextDrawImage(imageContext, self.bounds, cacheImage); // draws the current context;

[path removeAllPoints];

CGImageRelease(cacheImage); // releases the image to solve memory errors.

パスは私のUIBezierPathです。すべてのパスの設定は、タッチが開始され、タッチが移動してから[selfsetNeedsDisplay]を呼び出すことで行われます。drawRectを呼び出します。

何が起こっているのかというと、CGImageRefをコンテキストに適切に描画していないか、描画しているのですが、キャッシュイメージをキャプチャすると、パスだけでなく、どこかから白い背景がキャプチャされ、全体に貼り付けられます。最後のパスが白い背景の塗りつぶしと一緒に描画された画像。ビューの背景色がclearColorであっても、画像を構築するために描画された最後のパスを確認することはできません。

私は本当に理にかなっていることを願っています、私はこれにあまりにも多くの時間を費やしました、そしてそれは私を完全に使い果たしました。これが私が使っている描画方法です-

これは、画像コンテキストを作成します-

 -(CGContextRef) myCreateBitmapContext: (int) pixelsWide:(int) pixelsHigh{


    imageContext = NULL;

    CGColorSpaceRef colorSpace; // creating a colorspaceref
    void *          bitmapData; // creating bitmap data
    int             bitmapByteCount; // the bytes per count
    int             bitmapBytesPerRow; // number of bytes per row

    bitmapBytesPerRow   = (pixelsWide * 4); // calculating how many bytes per row the context     needs
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh); // how many bytes there are in     total

    colorSpace = CGColorSpaceCreateDeviceRGB(); // setting the colourspaceRef

    bitmapData = malloc( bitmapByteCount ); // calculating the data

    if (bitmapData == NULL)
    {
        //NSLog(@"Memory not allocated!");
        return NULL;
    }


    imageContext = CGBitmapContextCreate (bitmapData,
                                          pixelsWide,
                                          pixelsHigh,
                                          8, // bits per component
                                          bitmapBytesPerRow,
                                          colorSpace,kCGImageAlphaPremultipliedLast);

    if (imageContext== NULL)
    {
        free (bitmapData);
        NSLog(@"context not created allocated!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace ); //releasing the colorspace

    CGContextSetRGBFillColor(imageContext, 1.0, 1.0, 1.0, 0.0); // filling the bitmap with a     white background
    CGContextFillRect(imageContext, self.bounds);
    CGContextSetShouldAntialias(imageContext, YES); 




    return imageContext; 
    }

そして、ここに私の絵があります-

 -(void)drawRect:(CGRect)rect
{

    DataClass *data = [DataClass sharedInstance];



    [data.lineColor setStroke];
    [path setLineWidth:data.lineWidth];
    imageContext = UIGraphicsGetCurrentContext();

    UIGraphicsPushContext(imageContext);


    [path stroke];


    if(imageContext != nil){
        cacheImage = CGBitmapContextCreateImage(imageContext);  
    }


    CGContextScaleCTM(imageContext, 1, -1); // this one


    CGContextDrawImage(imageContext, self.bounds, cacheImage); // draws the current     context;



    [path removeAllPoints];

    CGImageRelease(cacheImage); // releases the image to solve memory errors.



}





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


    DataClass *data = [DataClass sharedInstance];

    CGContextSetStrokeColorWithColor(imageContext, [data.lineColor CGColor]);

    ctr = 0;
    UITouch *touch2 = [touches anyObject];
    pts[0] = [touch2 locationInView:self];

}


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

    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];


    ctr++;
    pts[ctr] = p;
    if (ctr == 4)
    {

        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move     the endpoint to the middle of the line joining the second control point of the first Bezier     segment and the first control point of the second Bezier segment
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a     cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
        //[data.lineColor setStroke];

        [self setNeedsDisplay];
        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }
}


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

    [path removeAllPoints];
    [self setNeedsDisplay];
    ctr = 0;
}

'path'は私のUIBezierPathです'cacheImage'はCGImageRefです'imageContext'はCGContextRefです

どんな助けでも大歓迎です!そして、あなたがそれを行うためのより良い方法を考えることができるならば、私に知らせてください!ただし、キャッシュイメージの背景を透明にする必要があるため、後でこれを機能させるときに何かを適用するため、パスだけが表示されます。

編集また、描画速度を上げるために毎回ポイントを削除しています。

前もって感謝します :)

4

1 に答える 1

1

さて、これは大きな質問です。潜在的なリードの1つは、必要なものを正確に描画すること(常に画像全体ではない)を確認し、複数のレイヤー間でアクティブに変化する領域/四角形から不変のビットマップを分割することです。

于 2012-11-02T21:44:11.723 に答える