非零环绕原则(nonzero winding number ru

2020-05-08  本文已影响0人  pluskok
- (void)drawRect:(CGRect)rect
{
    // Drawing code

    // 获取图片对象
    UIImage* image = [UIImage imageNamed:@"me"];

    // 获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 1.先画出来显示的区域
    CGContextAddArc(ctx, 150, 150, 150, 0, 2 * M_PI, 1);
    CGContextAddRect(ctx, CGRectMake(0, 0, 150, 150));
    CGContextAddRect(ctx, CGRectMake(150, 150, 150, 150));

//    CGContextFillPath(ctx);

        // 2.裁剪
        CGContextClip(ctx);
    
        // 拉伸显示到 view 上
        [image drawInRect:rect];
}

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-05-08 at 12.39.09.png

Demo中不理解图片为啥显示区域那样显示。查看API自己画一画就知道为何了。注意context中画图的轨迹顺时针还是逆时针。

CGContextClip

The function uses the nonzero winding number rule to calculate the intersection of the current path with the current clipping path. The path resulting from the intersection is used as the new current clipping path for subsequent painting operations.
If the current path includes any open subpaths, the paths are treated as if they were closed by calling CGContextClosePath.
Unlike the current path, the current clipping path is part of the graphics state. Therefore, to re-enlarge the paintable area by restoring the clipping path to a prior state, you must save the graphics state before you clip and restore the graphics state after you’ve completed any clipped drawing.
After determining the new clipping path, the function resets the context’s current path to an empty path.*/

非零环绕规则

【摘】 https://blog.csdn.net/Jianxq/article/details/78267666?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

CGContextAddRect: 从左下角开始逆时针画轨迹。

This is a convenience function that adds a rectangle to a path, starting by moving to the bottom left corner and then adding lines counter-clockwise to create a rectangle, closing the subpath.

上一篇下一篇

猜你喜欢

热点阅读