gpt4 book ai didi

iphone - 画线效果

转载 作者:行者123 更新时间:2023-12-03 17:37:18 28 4
gpt4 key购买 nike

我想在 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;
}

请。帮我。实际上我想要一些像线条光泽效果这样的效果。这在 cocoa 中是可能的。

最佳答案

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

那么,为什么不专用一个透明的 UIView 来绘图并将其放置在 UIImageView 上呢?绘制到它的 -drawRect 方法中。现在,如果您只为执行的每个绘制操作保留一个 NSMutableArray,那么在调用 drawRect 方法之前调用 [theArray removeLastObject] 或 [theArray removeAllObjects] 就可以轻松删除您绘制的内容。当您对单独的 UIView 上的绘图感到满意时,您可以通过将 UIView 添加为 UIImageView 的 subview 来组合两个 View ,然后从 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] 来绘制 View 。

关于iphone - 画线效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1346507/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com