drawRect:/renderInContext: issues

前端 未结 1 1482
旧时难觅i
旧时难觅i 2021-01-17 00:37

Seem to be having a bit of an issue trying to draw my view into a context.

My basic setup is, I have a view controller that owns a UIPageViewController

1条回答
  •  心在旅途
    2021-01-17 01:20

    You must not call [self.layer renderInContext:] in drawRect:, period. As you have figured out, renderInContext: calls drawRect: if it is implemented, which will cause an infinite recursion.

    You should render a book as is separately, then use the rendered image to let users draw on top of it. One way to do that is to enclose a self-contained drawing code in the UIGraphicsBeginImageContext()/UIGraphicsEndImageContext() block and use the resulting image in drawRect:. Another way is to use two different views which are not subviews of each other, one drawing an unaltered book, the other drawing user sketches.

    EDIT: first of all, you need an original image (book page, whatever). Draw it inside a UIGraphicsBeginImageContext()/UIGraphicsEndImageContext() block without calling [self.layer renderInContext:] and save it in the view's ivar (say, self.currentImage). In touchesMoved:withEvent: call setNeedsDisplay (it will call drawRect: for you) to update the view. Use self.currentImage in drawRect: as the background image.

    I hope I'm not too vague.

    0 讨论(0)
提交回复
热议问题