菜鸟教程小白 发表于 2022-12-12 12:34:50

ios - CGContextClipToMask 不剪辑


                                            <p><p>此代码生成下图。据我了解 CGContextClipToMask,红色矩形不应该是可见的,因为它在剪切区域之外。我在这里想念什么?感谢您的帮助!</p>

<pre><code>CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, .CGColor);
CGContextFillRect(context, rect);
CGContextSetLineWidth(context, 20);
CGContextSetStrokeColorWithColor(context, .CGColor);

// draw partial circle
UIBezierPath *arc   = ;
CGContextAddPath(context, );
CGContextStrokePath(context);

// create mask
CGImageRef mask = CGBitmapContextCreateImage(context);
self.maskCreated(mask);

// save state
CGContextSaveGState(context);

// clip with mask
CGContextClipToMask(context, rect, mask);

// draw test rect
CGContextSetFillColorWithColor(context, .CGColor);
CGContextFillRect(context, CGRectMake(0, 0, 100, 100));

// restore state
CGContextRestoreGState(context);
</code></pre>

<p> <img src="/image/W3Mmn.png" alt="Code Result"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><a href="https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGContext/index.html#//apple_ref/c/func/CGContextClipToMask" rel="noreferrer noopener nofollow">documentation</a>对于 <code>CGContextClipToMask</code> 说:</p>

<blockquote>
<p>If mask is an image, then it must be in the DeviceGray color space,
may not have an alpha component, and may not be masked by an image
mask or masking color.</p>
</blockquote>

<p>我假设您的代码位于 <code>UIView</code> 子类的 <code>-drawRect:</code> 方法中,因此您使用的是 <code>CGContext</code>这是提供给您的,它位于 RGB 颜色空间中,并且可能具有 alpha 分量。您的 <code>mask</code> 图像是根据该上下文创建的,因此它具有相同的属性。</p>

<p>要解决此问题,请使用单独的位图上下文生成蒙版,并使用没有 alpha 的灰色色彩空间。这是一个与您的代码类似的独立示例。</p>

<pre><code>- (void)drawRect:(CGRect)rect
{
    // Create a context for the mask
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef maskContext = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaNone | kCGBitmapByteOrderDefault);
    CGColorSpaceRelease(colorSpace);

    // Fill with black
    CGContextSetFillColorWithColor(maskContext, .CGColor);
    CGContextFillRect(maskContext, rect);

    // Draw an arc in white
    CGContextSetLineWidth(maskContext, 20);
    CGContextSetStrokeColorWithColor(maskContext, .CGColor);
    CGContextAddArc(maskContext, CGRectGetMidX(rect), CGRectGetMidY(rect), 50, M_PI, 0, false);
    CGContextStrokePath(maskContext);

    // Create the mask image from the context, and discard the context
    CGImageRef mask = CGBitmapContextCreateImage(maskContext);
    CGContextRelease(maskContext);

    // Now draw into the view itself
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Apply the mask
    CGContextClipToMask(context, rect, mask);

    // Then draw something that overlaps the mask
    CGContextSetFillColorWithColor(context, .CGColor);
    CGContextFillRect(context, rect);

    // Make sure to clean up when we&#39;re done
    CGImageRelease(mask);
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CGContextClipToMask 不剪辑,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17721959/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17721959/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CGContextClipToMask 不剪辑