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?
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.