tags:

views:

567

answers:

1

I want to draw line on UIImageView. I already draw line on UIImageView but when i want to remove these line 7 redraw new lines That can not happen.

-(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. help me. Actually i want some Effect Like line glossy Effect. This is possible in cocoa.

+3  A: 

I hope I am understanding your question correctly. You want to draw a line on a UIImageView and be able to delete that line afterward, correct?

Well, why not dedicate a transparent UIView just for drawing and position it over the UIImageView? Draw into its -drawRect method. Now, if you just keep an NSMutableArray of each draw operation you perform it becomes easy to delete what you drew by calling [theArray removeLastObject] or [theArray removeAllObjects] before your drawRect method is called. When you are satisfied with your drawing on the seperate UIView you can then combine the two views by adding the UIView as a Subview of the UIImageView, and then get a "flattened" UIImage from the UIImageView as follows:

//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();

Now you have a new UIImage that you can add to UIImageViews and do whatever you want with it.

IMPORTANT:
1) Each time your -drawRect method is called, all of your drawings are erased.
2) You don't call -drawRect directly. Use [self setNeedsDisplay] instead, to draw your view.

RexOnRoids