为CGLayer创建CGContext [英] Create CGContext for CGLayer

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

问题描述

我想将某些图形预渲染到CGLayer中,以便将来快速绘制.

I want to pre-render some graphics into CGLayer for fast drawing in future.

我发现CGLayerCreateWithContext需要一个CGContext参数.在drawRect:方法中可以轻松找到它.但是我需要在drawRect:之外创建一个CGLayer.我应该在哪里得到CGContext?

I found that CGLayerCreateWithContext requires a CGContext parameter. It can be easily found in drawRect: method. But I need to create a CGLayer outside of drawRect:. Where should I get CGContext?

我应该只是创建临时CGBitmapContext并使用它吗?

Should I simply create temporary CGBitmapContext and use it?

更新: 我需要在drawRect之外创建CGLayer:因为我想在渲染之前初始化CGLayer.可以在第一次drawRect调用时初始化一次,但这对我来说不是一个漂亮的解决方案.

UPDATE: I need to create CGLayer outside of drawRect: because I want to initialize CGLayer before it is rendered. It is possible to init once on first drawRect call but it's not beautiful solution for me.

推荐答案

没有理由在drawRect:之外进行操作,实际上,在内部进行操作有一些好处.例如,如果您更改视图的大小,则该图层仍将以正确的大小制作(假设它是基于视图的图形上下文,而不仅仅是任意大小).这是一种常见的做法,我认为在外部创建它不会有任何好处.无论如何,大部分CPU周期都将花费在CGContextDrawLayer中.

There is no reason to do it outside of drawRect: and in fact there are some benefits to doing it inside. For example, if you change the size of the view the layer will still get made with the correct size (assuming it is based on your view's graphics context and not just an arbitrary size). This is a common practice, and I don't think there will be a benefit to creating it outside. The bulk of the CPU cycles will be spent in CGContextDrawLayer anyway.

这篇关于为CGLayer创建CGContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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