如何访问CGContext以在屏幕外创建CGLayer [英] How to access CGContext for creating CGLayer offscreen

查看:79
本文介绍了如何访问CGContext以在屏幕外创建CGLayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

意图:在屏幕外创建CGLayer,从第二个线程在其上绘制一些复杂的东西,并稍后用于在主线程上快速绘制

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

问题: CGLayerCreateWithContext(context, size, info)期望已经存在的CGContext,以便它知道需要针对哪种上下文进行优化.

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

我到目前为止发现的解决方案: CGContextRef ctx = UIGraphicSetCurrentContext() 但似乎不再存在此功能.

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

问题:是否没有另一种方法来访问诸如默认上下文之类的内容?还是我真的需要等待第一个drawRect:调用只是为了访问UIGraphicsGetCurrentContext()并从主线程创建所有CGLayers而浪费了绘图运行?

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?

推荐答案

您可以通过执行以下操作来创建图像上下文:

you can create an image context by doing something like:

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

这样说,我不确定您是否可以从main以外的线程中执行此操作.不过值得一试.

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

这篇关于如何访问CGContext以在屏幕外创建CGLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆