2012-04-19 35 views
7

Tôi đang tạo ứng dụng vẽ và tôi muốn biết cách triển khai công cụ tẩy. Tôi không muốn có công cụ tẩy để tô màu trắng vì tôi muốn cho phép người dùng thay đổi màu nền. Và cũng có thể đặt độ cứng của bàn chải? Nếu có, xin vui lòng cho tôi biết làm thế nào.Tạo công cụ tẩy cho ứng dụng vẽ trong iOS

Cảm ơn bạn

Dưới đây là những gì tôi đã làm như vậy cho đến nay:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    lastPoint = [touch locationInView:self.view]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 
UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 
} 
- (IBAction)clear:(id)sender { 
drawImage.image = nil; 
} 
+0

Đây là một câu hỏi thú vị nhưng tôi nghĩ bạn có thể làm cho nó rất nhiều rõ ràng hơn. Hãy cho chúng tôi biết những gì bạn có cho đến nay; có lẽ bao gồm một số mã. – jtbandes

+0

@jtbandes Tôi đã thêm mã –

+0

vui lòng xem liên kết này và xem câu trả lời của tôi http://stackoverflow.com/questions/3863931/want-to-add-manual-erasing-option-in-ipad-painting- ứng dụng-by-thạch anh/12797513 # 12797513 –

Trả lời

13

Ok đây là những gì tôi đã làm cho công cụ tẩy:
tôi thêm dòng mã này:
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

Vì vậy, các mã sẽ được một cái gì đó như thế này:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 
UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

// I add this 
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); 

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), sizeSlider.value); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 

CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
lastPoint = currentPoint; 
} 
+3

Cảm ơn rất nhiều. Nó hoạt động tuyệt vời. Tôi đã tìm kiếm điều này từ rất lâu rồi. –

+0

Cảm ơn bạn rất nhiều :) – iRiziya

4

tôi có làm đơn sơn và nó có sẵn trên iTunes "Easy Doodle" App. Không có logic đặc biệt cho tẩy. chỉ nhận giá trị RGB của hình nền và vượt qua nó trong

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),R value,G value,B value, 1.0); 

theo hình nền được chọn.

+0

@mat hi .. nếu chúng ta tạo UIView tùy chỉnh cho sơn hơn chúng ta có thể vẽ mà không có drawrect mehod? – Hitarth

+0

Có lẽ bạn muốn hỏi Rock, nhưng tôi không nghĩ vậy, nếu bạn có UIView tùy chỉnh, bạn cần phải gọi setNeedsDiplay để vẽ bản thân mỗi lần bạn muốn sửa đổi nó. Nếu không, hãy xem [phản hồi tương tự] cũ của tôi (http://stackoverflow.com/a/10667557/251513). – Mat

+1

Đây không phải là câu trả lời đúng. Câu trả lời đúng là câu trả lời dưới đây với nhiều phiếu bầu hơn. Apple đã tích hợp chức năng tẩy trong đó là một dòng mã. Câu trả lời này thậm chí không thực sự xóa, nó chỉ đơn giản là thêm nhiều hơn "sơn" cho hình ảnh, chỉ là cùng một màu sắc như các điểm ảnh bên dưới nó (hình nền). Không thực sự xóa (đặc biệt nếu hình nền thay đổi) và nhiều dữ liệu nặng hơn. –

1

Ok vậy Tôi đã tìm kiếm một giải pháp về cách tạo một cục tẩy trong một tuần, tôi phải thay đổi cách tôi nhìn vào nó bất kể có bao nhiêu lần tôi cố xoá một thứ gì đó cũng sẽ xóa hình nền. Tuy nhiên, cuối cùng tôi đã tìm thấy một giải pháp phù hợp với tôi (nơi tôi có thể xóa sơn và vẫn có nhận thức về hình nền không bị ảnh hưởng hoặc bị xóa) và hy vọng nó sẽ giúp người khác. Vì vậy, ở đây đi ...

1) Tạo 2 imageviews trong UIView 2) Đặt cả imageviews với hình nền mà bạn muốn nó được ... 3) tôi đã tạo một điều khiển phân đoạn để xác định xem người sử dụng muốn xóa 4) thêm chức năng sau khi kiểm soát phân đoạn được chọn ..

func drawLineForRubber(fromPoint: CGPoint, toPoint: CGPoint) { 

    // 1 
    UIGraphicsBeginImageContext(view.frame.size) 
    let context = UIGraphicsGetCurrentContext() 
    mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)) 

    // 2 
    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y) 
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y) 

    // 3 
    CGContextSetBlendMode(context, CGBlendMode.Clear) 
    CGContextSetLineCap(context, CGLineCap.Round) 
    CGContextSetLineWidth(context, 10.0) 
    //this line is very important as it paints the screen clear 
    CGContextSetRGBStrokeColor(context, red, green, blue, 0.0) 
    CGContextSetBlendMode(context, CGBlendMode.Clear) 

    // 4 
    CGContextStrokePath(context) 

    // 5 
    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext() 
    mainImageView.alpha = opacity 
    UIGraphicsEndImageContext() 

} 

Như bạn sẽ thấy có một dòng trong đó mà đặt CGContextSetRGBStrokeColor với giá trị 0.0, đây là nguyên nhân quan trọng khi người dùng đang xóa lớp sơn, anh ấy đang xóa lớp sơn khỏi hình ảnh thu nhỏ trên cùng cùng với lớp sơn có màu sắc rực rỡ r. Tuy nhiên, vì bạn có một lần xem hình ảnh khác bên dưới nó, nhận thức là có vẻ như nó bị chà xát mà không ảnh hưởng đến hình nền.

Ngược lại, nó được cọ xát nhưng hình ảnh phía sau, làm cho nó trông giống như nó không có. Khi bạn muốn xuất hình ảnh (Bạn có thể thực hiện theo một số cách nhưng tôi đã làm theo các bước sau) bạn đã vẽ hình nền chỉ cần kết hợp cả hai chế độ xem hình ảnh với số ảnh thứ nhất sẽ có sơn bạn vẽ và Lần xem thứ hai sẽ có hình nền ban đầu mà bạn muốn.

func share() { 

    let layer = UIApplication.sharedApplication().keyWindow!.layer 
    let scale = UIScreen.mainScreen().scale 
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale); // reconsider size property for your screenshot 

    layer.renderInContext(UIGraphicsGetCurrentContext()!) 
    let screenshot = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 


    UIGraphicsBeginImageContext(mainImageView.bounds.size) 
    mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, 
     width: mainImageView.frame.size.width, height: mainImageView.frame.size.height)) 

    UIGraphicsEndImageContext() 

    let activity = UIActivityViewController(activityItems: [screenshot], applicationActivities: nil) 
    presentViewController(activity, animated: true, completion: nil) 
} 

Hy vọng điều này giúp những người khác trên đường đi .. người đàn ông nó đã cho tôi lứa tuổi để có được đầu của tôi xung quanh nó =)

Các vấn đề liên quan