How to access CGContext for creating CGLayer offscreen

不想你离开。 提交于 2019-12-23 16:26:16

问题


Intention: creating CGLayer offscreen, draw some complicated stuff on it from second thread and use it later for quick drawing on main thread

Problem: CGLayerCreateWithContext(context, size, info) expects an already existing CGContext so that it knows for what kind of context it needs to be optimized.

Solution I found so far: CGContextRef ctx = UIGraphicSetCurrentContext() but this function doesn't seem to exist anymore.

Question: Isn't there another way to access something like a default context? Or do i really need to wait for the first drawRect: call just for accessing UIGraphicsGetCurrentContext() and creating all CGLayers from main thread with one wasted drawing run?


回答1:


you can create an image context by doing something like:

UIGraphicsBeginImageContext(rect);
    // your drawing code
UIGraphicsEndImageContext();

with that said, i'm not sure you can do this from a thread other than main. worth a try, though.



来源:https://stackoverflow.com/questions/8637860/how-to-access-cgcontext-for-creating-cglayer-offscreen

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