2009-08-28 4 views
0

UIImageViewで線を描きたい。私はすでにUIImageViewに線を描いていますが、これらの線7を削除したいときは新しい線を描き直すことはできません。線画効果

-(void) drawLine 

{ 

    lastPoint = currentPoint; 
    lastPoint.y -= 40; 

    //drawImage is a UIImageView declared at header 
    UIGraphicsBeginImageContext(drawImage.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; 

    //sets the style for the endpoints of lines drawn in a graphics context 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetLineCap(ctx, kCGLineCapButt); 
    //sets the line width for a graphic context 
    CGContextSetLineWidth(ctx,2.0); 
    //set the line colour 
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
    //creates a new empty path in a graphics context 
    CGContextBeginPath(ctx); 
    //begin a new path at the point you specify 
    CGContextMoveToPoint(ctx, currentPoint.x, currentPoint.y); 
    //Appends a straight line segment from the current point to the provided point 
    CGContextAddLineToPoint(ctx, currentPoint.x,lastPoint.y); 
    //paints a line along the current path 
    CGContextStrokePath(ctx); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    currentPoint = lastPoint; 
} 

Pls。助けて。実際に私はいくつかのエフェクトラインの光沢効果と同じようにしたい。これはココアで可能です。

+2

あなたの質問は非常に理解しにくいです。グラデーションラインを探している場合は、次の質問を参考にしてください。http://stackoverflow.com/questions/1303855/how-to-draw-a-gradient-line-fading-in-out-with-core-graphics -iphone –

答えて

3

あなたの質問を正しく理解していただきたいと思います。あなたは、UIImageView上に線を描画し、その線を後で削除できるようにしたいですか?

なぜ、透明なUIViewを描画専用にしてUIImageView上に配置するのはどうですか?その-drawRectメソッドに描画します。これで、実行する各描画操作のNSMutableArrayを保持すれば、drawRectメソッドが呼び出される前に、[theArray removeLastObject]または[theArray removeAllObjects]を呼び出すことによって、描画したものを簡単に削除することができます。あなたが別々のUIView上の図面に満足しているときは、UIImageViewのサブビューとしてのUIViewを追加することによって、二つのビューを組み合わせることができ、その後、次のようにUIImageViewからUIImageを「フラット化」を取得:

//myImageView is your UIImageView 
//myDrawingView is the transparent UIView that you drew on 

[myImageView addSubview:myDrawingView]; 

//Create new image container to hold the flattened image 
UIImage* newImage; 

    //Now flatten the graphics 
    UIGraphicsBeginImageContext(myImageView.bounds.size); 
    [myImageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

をこれでUIImageViewsに追加できる新しいUIImageができました。重要

は:
1)あなた-drawRectメソッドが呼び出されるたびに、あなたの図面のすべてが消去されます。
2) -drawRectを直接呼び出すことはありません。代わりに[self setNeedsDisplay]を使用して、ビューを描画します。

関連する問題