drawRect:/renderInContext: issues

谁都会走 提交于 2019-12-01 14:37:14

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.

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