Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS CoreGraphics: Stroking with semi-transparent patterns leads to colors corruption
    primarykey
    data
    text
    <p>My task is to make something like an erasing tool (operated with a finger) that will reveal a background image instead of an erased image.</p> <p>Here are my source and destination images (just for test, real ones will differ):</p> <p><img src="https://i.stack.imgur.com/EJIiM.png" alt="image1"> <a href="http://img232.imageshack.us/img232/6030/29572847.png" rel="nofollow noreferrer">http://img232.imageshack.us/img232/6030/29572847.png</a></p> <p>And here's my code. Creating the pattern:</p> <pre><code>- (void)setFrame:(CGRect)frame { [super setFrame:frame]; if(revealPattern) CGPatternRelease(revealPattern); CGPatternCallbacks callbacks = { 0, &amp;patternCallback, NULL}; revealPattern = CGPatternCreate(self, self.bounds, CGAffineTransformIdentity, self.bounds.size.width, self.bounds.size.height, kCGPatternTilingConstantSpacing, true, &amp;callbacks); } </code></pre> <p>Pattern callback function (**info* contains the pointer to <em>self</em>):</p> <pre><code>void patternCallback(void *info, CGContextRef context) { CGRect rect = ((DrawView *)info).bounds; CGImageRef imageRef = ((DrawView *)info).backgroundImage.CGImage; CGContextTranslateCTM(context, 0, rect.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextDrawImage(context, rect, imageRef); } </code></pre> <p>And the drawing method:</p> <pre><code>- (void)drawPoints:(CGContextRef)context{ if([points count] &lt; 2) return; CGPoint lastPoint = [[points objectAtIndex: 0] CGPointValue]; CGContextBeginPath(context); CGContextMoveToPoint(context, lastPoint.x, lastPoint.y); CGContextSetBlendMode(context, kCGBlendModeNormal); CGColorSpaceRef revealPatternSpace = CGColorSpaceCreatePattern(NULL); CGContextSetStrokeColorSpace(context, revealPatternSpace); CGFloat revealAlpha = 1.0; CGContextSetStrokePattern(context, revealPattern, &amp;revealAlpha); CGContextSetAlpha(context, 0.1); CGColorSpaceRelease(revealPatternSpace); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineJoin(context, kCGLineJoinRound); CGContextSetLineWidth(context, self.lineWidth); for(int i = 1; i &lt; [points count]; i++){ CGPoint currPoint = [[points objectAtIndex: i] CGPointValue]; CGContextAddLineToPoint(context, currPoint.x, currPoint.y); lastPoint = currPoint; } CGContextStrokePath(context); } </code></pre> <p>If the local variable <strong>revealAlpha</strong> has value <strong>1.0</strong> (as shown in the last code piece), everything works fine. But I need the erasing to be semi-transparent (so that a user needs to swipe the same place several times to erase it fully). And here the problem appears. If I make <strong>revealAlpha</strong> to be less than <strong>1.0</strong>, the revealed destination image looks corrupted.</p> <p>Here are the results for <strong>revealAlpha == 1.0</strong> and <strong>revealAlpha == 0.1</strong>:</p> <p><img src="https://i.stack.imgur.com/C5Hri.png" alt="image2"> <a href="http://img593.imageshack.us/img593/1809/69373874.png" rel="nofollow noreferrer">http://img593.imageshack.us/img593/1809/69373874.png</a></p> <p>If you look close enough, you will notice that the blue gradient on the right picture is not smooth anymore.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload