1

描かれた道をuiimageに保存する方法がわかりませんか?
uigraphicsbeginimageContextなどを使用する必要があることを知っています。
しかし、私はそれを私のタッチ機能のどこに置くべきかわかりません。

すべてのパスを画像に保存する必要があります。なぜなら、描かれた各ラインの後に色とアルファの値を変更できるようにしたいからです。
このコードを使用すると、値を変更すると同時に、すべての行が色と幅で調整されます。

- (id) initWithFrame:(CGRect)frame andImage: (UIImageView*) image
{
    ...
    paths = [[NSMutableArray alloc]init];

    drawImage = image;
    self.backgroundColor=[UIColor clearColor];
    [self addSubview:drawImage];
}
- (void)drawRect:(CGRect)rect
{
    [brushPattern setStroke];
    [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    // Drawing code
    //[myPath stroke];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    myPath=[[UIBezierPath alloc]init];
    [myPath moveToPoint:[mytouch locationInView:self]];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];
}

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

    [paths addObject:myPath];
    [myPath release];
}
4

2 に答える 2

3

以前の質問に基づいて、画像ビューはこのカスタム ビュー オブジェクトのサブビューであると想定しています。このクラスでは、変更可能なパスの配列を維持する必要があります。で初期化し、でtouchesBegan:withEvent:パスを閉じますtouchesEnded:withEvent:。パスを配列にプッシュします。タッチ イベント (開始、移動、終了) ごとに、タッチ ポイントをパスに追加した後にイメージ ビューのイメージを更新するメソッドを呼び出します。

画像を更新するには、空の画像コンテキストを作成し、配列内のすべてのパスと現在のパスをストロークしながら反復処理する必要があります。描画が完了したら、コンテキストから画像オブジェクトを生成し、画像に設定します。各パスの属性が異なる可能性があることに気付くまでは、非常に簡単です。これに対処するには、パスに関する属性を保持するパスのコンテナー クラスを作成する必要があります。パス自体ではなく、すべてのパスのこのクラスのオブジェクトを配列にプッシュします。このようにして、ビューの状態を維持できます。

于 2011-06-07T22:09:17.503 に答える
1

1 つの方法と非常に単純な事実があります。パス オブジェクトを描画するたびに、それをインクリメンタル イメージに保存できます。必要な場所に作成し、このイメージを描画 rect で表示してから、他の何かを描画します。

サンプルコードは次のとおりです。

  - (void)drawBitmap 
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
    [strokeColor setStroke];
    if (!incrementalImage) // first time draw; paint background white by ...
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor colorWithPatternImage:[UIImage imageNamed:@"two.jpg"]] setFill];
        //[[UIColor whiteColor] setFill];
        [rectpath fill]; // filling it with white
        NSLog(@"========== ... drawBitmap .. CALLED=======");
    }
    [incrementalImage drawAtPoint:CGPointZero];

    for(NSDictionary *_pathDict in pathArray)
    {
        [((UIColor *)[_pathDict valueForKey:@"color"]) setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        [[_pathDict valueForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    }

その後、 drawRect: でこのインクリメンタル イメージを呼び出すだけで、ユーザーは連続描画の錯覚を得ることができ、このイメージの時点を保存できます。

    - (void)drawRect:(CGRect)rect
{

     [incrementalImage drawInRect:rect];
     [[dict_path objectForKey:@"color"] setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)

     [[dict_path objectForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

}

必要に応じてコードを変更する必要があるかもしれません。これが誰かの助けになることを願っています

于 2013-02-08T06:41:46.393 に答える