如何在简单的图形编辑器中实现橡皮擦?

发布于 2024-12-03 02:24:18 字数 1331 浏览 1 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-12-10 02:24:18

您可以将橡皮擦实现为一支笔,用完全透明的颜色进行绘制(将 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.

音盲 2024-12-10 02:24:18

用背景颜色绘画将起到橡皮擦的作用。

Painting with the background color will work as an eraser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文