线条效果

时间:2009-08-28 11:46:21

标签: iphone cocoa

我想在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。帮我。其实我想要一些效果像线条光泽效果。这在可可中是可能的。

1 个答案:

答案 0 :(得分:3)

我希望我能正确理解你的问题。你想在UIImageView上画一条线,然后能够删除那条线,对吗?

那么,为什么不专门用于绘制透明UIView并将其放置在UIImageView上?绘制其-drawRect方法。现在,如果您只保留每个绘制操作的NSMutableArray,则在调用drawRect方法之前调用[theArray removeLastObject]或[theArray removeAllObjects]可以很容易地删除您绘制的内容。当您对单独的UIView上的绘图感到满意时,您可以通过添加UIView作为UIImageView的子视图来组合两个视图,然后从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();

现在你有了一个新的UIImage,可以添加到UIImageViews并用它做你想做的任何事情。

重要事项:
1)每次调用-drawRect方法时,所有绘图都将被删除 2)您不直接调用-drawRect。请改用[self setNeedsDisplay]来绘制视图。