Ios 如何在简单的图形编辑器中实现橡皮擦?
我想在iOS上创建简单的图形编辑器,在其中我使用位图图形上下文和贝塞尔路径将线条转换为图片 我没有找到一种方法来制作橡皮擦 你有什么想法吗Ios 如何在简单的图形编辑器中实现橡皮擦?,ios,graphics,bitmap,uibezierpath,Ios,Graphics,Bitmap,Uibezierpath,我想在iOS上创建简单的图形编辑器,在其中我使用位图图形上下文和贝塞尔路径将线条转换为图片 我没有找到一种方法来制作橡皮擦 你有什么想法吗 - (void)drawRect:(CGRect)rect { [[UIColor blackColor] setStroke]; CGContextRef aRef = UIGraphicsGetCurrentContext(); aPath.lineWidth = 5; [aPath stroke]; [[[U
- (void)drawRect:(CGRect)rect {
[[UIColor blackColor] setStroke];
CGContextRef aRef = UIGraphicsGetCurrentContext();
aPath.lineWidth = 5;
[aPath stroke];
[[[UIColor blackColor] colorWithAlphaComponent:0] setStroke];
bPath.lineWidth = 5;
[bPath stroke];
movesss= CGBitmapContextCreateImage(aRef);
if (movesss==NULL) {
NSLog(@"null =(");
}
}
-(void)moveTo:(CGPoint)pos {
if(del){
[bPath moveToPoint:pos];
}
else{
[aPath moveToPoint:pos];
}
}
-(void)cret{
aPath=[[UIBezierPath bezierPath] retain];
bPath=[[UIBezierPath bezierPath] retain];
del=NO;
}
-(void)lineTo:(CGPoint)pos {
if(del){
[bPath addLineToPoint:pos];
}
else{
[aPath addLineToPoint:pos];
}
[self setNeedsDisplay];
}
-(void)imgf{
UIImage *imageToSave=[[UIImage imageWithCGImage:movesss] retain];
UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
UIImageView *trew=[[UIImageView alloc] initWithFrame:[self frame]];
[trew setBackgroundColor:[UIColor greenColor]];
[trew setImage:imageToSave];
[self addSubview:trew];
}
-(void)swittch{
del=!del;
}
这里出了什么问题?您可以实现一个橡皮擦作为一支笔,使用完全透明的颜色进行绘制(将alpha设置为零)。拥有一个“alpha”参数,可以在创建时使用colorWithAlphaComponent进行设置:例如 或者只使用
UIColor*color=[UIColor clearColor]代码>
编辑:
我认为用清晰的颜色绘画会用清晰的像素来代替像素。这是愚蠢的推理,因为如果是这样的话,多次使用阿尔法ed颜色进行绘制将无法正常工作
根据这一点,透明颜色不适用于此目的。你需要知道你的背景色并用它来画画。如果你的背景颜色是“透明的”。。。我不知所措。用背景色画画会起到橡皮擦的作用。我听从了你的建议,但不起作用:线条仍然是黑色的。谢谢你的链接。我的背景色是透明的。我只需要像这样更改混合模式:CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);