Iphone 如何使用cgcontext绘制nil
我想画Iphone 如何使用cgcontext绘制nil,iphone,uiimageview,cgcontext,null,Iphone,Uiimageview,Cgcontext,Null,我想画nil而不是颜色,因为最后我想让代码删除UIImageView的一部分,以便您可以看到UIImageView后面的内容 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view]; currentPoint
nil
而不是颜色,因为最后我想让代码删除UIImageView
的一部分,以便您可以看到UIImageView
后面的内容
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.x = currentPoint.x;
currentPoint.y = currentPoint.y;
mouseSwiped = YES;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal);
CGContextSetAlpha(UIGraphicsGetCurrentContext(), drawingColorAlpha);
CGContextSaveGState(UIGraphicsGetCurrentContext()); //Probably right here
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),
drawingColorRed, // I want to draw nil here instead of these CGFloats that I have made
drawingColorGreen, // I want to draw nil here instead of these CGFloats that I have made
drawingColorBlue, // I want to draw nil here instead of these CGFloats that I have made
drawingColorAlpha); // I want to draw nil here instead of these CGFloats that I have made
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(),
lastPoint.x,
lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),
currentPoint.x,
currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing my context to
NSLog(@"current point x: %d current point y: %d",currentPoint.x, currentPoint.y); //Used for my purposes
lastPoint = currentPoint;
mouseMoved++;
及
你想要的不是零,而是一种透明的颜色。如果UIImageView在IB中未标记为“不透明”,则它应在像素透明的任何区域显示。您要做的是将
drawingColorAlpha
设置为0。这将创建一种透明颜色,相当于擦除。您可能还必须使用CGContextSetBlendMode()
设置混合模式<代码>kCGBlendModeNormal应该可以正常工作。科林·吉斯拉森的答案不正确。如果将drawingColorAlpha
设置为0,它只会在现有图像上绘制一种不可见的颜色
以下是有效的方法
//set this at the beginning
CGContextSetLineCap(ctx,kCGImageAlphaNone);
//after moving the point to your pen location:
CGContextClearRect (ctx, CGRectMake(lastPoint.x, lastPoint.y, 10, 10));
按如下方式设置混合模式:
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear)
希望它能帮助你!!:P不,实际上我想让它删除UIImageView的一部分我想你没有理解我说的话。如果在图像视图中绘制透明像素,则其后面的任何内容都将显示出来。
//set this at the beginning
CGContextSetLineCap(ctx,kCGImageAlphaNone);
//after moving the point to your pen location:
CGContextClearRect (ctx, CGRectMake(lastPoint.x, lastPoint.y, 10, 10));