How to set up a clipping rectangle or area

眉间皱痕 提交于 2019-12-11 04:26:35

问题


How does one set up, modify, and clear a clipping rectangle inside a iOS drawRect for clipping generic CG drawing of lines, text, images, etc. to a small portion of a view?

Is it possible to use a more complex clipping region that is a composite of a bunch of rectangles and circles?


回答1:


You can set a clipping area with arbitrary paths, not restricted to rectangles. The followings are some ways of doing it:

  1. You can draw an arbitrary path and set a clipping area with it. For example:

    CGContextBeginPath(context);
    //draw a path here
    CGContextClosePath(context);
    CGContextClip(context);
    //following drawing on the context will be clipped
    
  2. If you want to use a mask image to set a clipping area, use CGContextClipToMask method.

See Apple's 'QuartzDemo' sample project for more usages.




回答2:


If you just want to clip to a rect you can use:

CGContextClipToRect(context, clipRect);


来源:https://stackoverflow.com/questions/5451838/how-to-set-up-a-clipping-rectangle-or-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!