问题
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