2011-09-07 37 views
1

我想在iOS上創建簡單的圖形編輯器,其中我使用位圖圖形上下文和bezier路徑將行轉換爲圖片。如何在簡單的圖形編輯器中實現橡皮擦?

我沒有找到方法來創建一個橡皮擦

你有什麼想法嗎?

- (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; 
} 


這裏有什麼錯?

回答

1

您可以使用完全透明的顏色(將alpha設置爲零)實現橡皮擦作爲筆。例如,UIColor擁有一個「alpha」參數,可以在創建時使用colorWithAlphaComponent進行設置。

或者只是使用UIColor * color = [UIColor clearColor];

編輯:

我想畫clearColor將有明確的像素替換像素。這是愚蠢的推理,因爲如果是這種情況,多次使用阿爾法顏色繪畫將無法正常工作。根據question,透明顏色不適用於此目的。你需要知道你的背景顏色並用它來畫。如果你的背景色是「透明的」......我不知所措。

+0

我按照你的意見,但它不起作用:線條仍然是黑色的。 – Riddick

+2

謝謝你的鏈接。我的背景色是透明的。我只需要像這樣改變混合模式:CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy); – Riddick

1

用背景顏色繪畫將作爲橡皮擦。