0

我想检测 uiimageview 的透明部分。我正在使用此代码绘制到 uiimageview。但我想检测 uiimage 中的透明部分并防止将颜色填充到这些部分中。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 //   CGFloat red,green,blue,alpha;

    UITouch *touch1 = [touches anyObject];
    CGPoint currentPoint = [touch1 locationInView:Image_apple];

UIGraphicsBeginImageContext(Image_apple.frame.size);
    [Image_apple.image drawInRect:CGRectMake(0, 0, Image_apple.frame.size.width, Image_apple.frame.size.height)];

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);

    //   CGContextSetBlendMode(UIGraphicsGetCurrentContext(),[self getBlendMode]);

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x , lastPoint.y  );
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x , currentPoint.y  );
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),8.0 );

    // [[self getPaintColor] getRed:&red green:&green blue:&blue alpha:&alpha];
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0.5, 0.2, 1);

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    CGContextStrokePath(UIGraphicsGetCurrentContext());
    Image_apple.image = UIGraphicsGetImageFromCurrentImageContext();
    [Image_apple setAlpha:1];
    UIGraphicsEndImageContext();
}

提前致谢

4

1 回答 1

1

有一种混合模式可能对您有所帮助:kCGBlendModeDestinationOver. 它仅利用上下文的不透明部分。

如果您必须使用其他混合模式,您可以将图像转换为蒙版并将上下文剪辑到其中。

于 2013-09-03T06:39:45.527 回答