如何在简单的图形编辑器中实现橡皮擦?
我想在 iOS 上创建简单的图形编辑器,在其中使用位图图形上下文和贝塞尔曲线路径将线条转换为图片。
我没有找到创建橡皮擦的方法。。
你有什么想法吗?
- (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;
}
这是怎么回事?
I want to create simple graphics editor on iOS, in which I use bitmap graphics context and bezier paths to convert lines to pictures.
I didn't find a way to create an eraser.
Do you have any ideas?
- (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;
}
What's wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将橡皮擦实现为一支笔,用完全透明的颜色进行绘制(将 alpha 设置为零)。 UIColor 拥有“alpha” " 参数,可以在创建时使用 colorWithAlphaComponent: 例如进行设置。
或者只是使用
UIColor * color = [UIColorclearColor];
编辑:
我认为用clearColor绘画会用透明像素替换像素。这是愚蠢的推理,因为如果是这种情况,使用 alpha 颜色多次绘制将无法正常工作。
根据这个问题,透明颜色不能用于此目的。你需要知道你的背景颜色并用它来绘画。如果你的背景颜色是“透明”......我不知所措。
You could implement an eraser as a pen that paints with a fully transparent color (set alpha to zero). UIColor possesses a "alpha" parameter, that can be set at creation with colorWithAlphaComponent: for example.
Or just use
UIColor * color = [UIColor clearColor];
EDIT:
I thought painting with clearColor would replace the pixels with clear pixels. It was dumb reasoning since painting several times with alpha-ed colors would not work properly if it were the case.
According to this question, transparent colors don't work for that purpose. You need to know your background color and paint with it. If your background color is "transparent"... I'm at a loss.
用背景颜色绘画将起到橡皮擦的作用。
Painting with the background color will work as an eraser.