本文介绍了CGContext的静态背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是我在这里的第一个问题,所以大家好。我花了一些时间来解决以下问题,无法解决。如果有人可以向我指出正确的方向,我会很高兴。this is my first question here, so hello everyone. I have spent some time now wrapping my mind around the following problem and could not come up with a solution. I'd be happy if someone could point me in the right direction.我正在使用 CGContext 绘制实时数据的各种图形表示层,应以至少50fps的合理帧速率刷新。我想使用静态图像作为背景。在每个周期使用上下文重绘此图像会大大降低帧速率。我考虑过只使用 UIImageView 一次显示图像。这里的问题是,我想使用 CGContext 提供的blendmodes来获得更复杂的覆盖,而不仅仅是为每个图层设置alpha值。由于显示背景图像的 UIImageView 不是上下文的一部分,因此不受混合模式的影响。有谁知道如何在不过度影响帧速率的情况下实现所需的行为?谢谢您的帮助。I'm using CGContext to draw various layers of graphical representations of live data which should be refreshed at a reasonable framerate of at least 50fps. I want to use a static image as a background. Re-drawing this image using the context for every cycle significantly slows down the frame-rate. I thought about just using UIImageView to display the image once. The problem here is that I want to use the blendmodes provided by CGContext in order to achieve more sophisticated overlays than just setting an alpha-value for each layer. Since the UIImageView displaying the background image is not part of the context, it's not affected by the blendmodes. Does anyone have an idea how to achieve the desired behavior without affecting the frame-rate too much? Thanks for your help.推荐答案使用调用 CGContextDrawImage 与CGRect具有相同尺寸的code> CGImageRef 很快。设置一个 CGImageRef ,其背景图像的运行频率比每一帧都要少,例如视图的初始化代码。如果此背景图像正在更改,请使用程序逻辑代码(而不是绘制刷新代码)构建图像。A call to CGContextDrawImage with a CGImageRef with the the same size dimensions as the CGRect is fast. Setup a CGImageRef with the background image in code that gets run less often than every frame, such as the view's initialization code. If this background image is changing, build the image in the program logic code, not the draw refresh code.最糟糕的事情是每次都可以从文件访问图像。通过不从较重的对象中访问 CGImageRef 可以节省一些时间。像 [someUIImageObject CGImage] 这样的调用会增加消息发送的开销,而访问 CGImageRef 类型的变量The worst you can do is access the image from a file each time. Some time can be saved by not accessing the CGImageRef from within a heavier object. A call like [someUIImageObject CGImage] will add the overhead of a message send, whereas an access to a CGImageRef type ivar in the same object containing the drawing code will not.如果您的源图像尺寸不合适,请首先将其绘制到 CGImageRef 一次达到所需大小,然后在您的图形代码中重复使用。If your source image is not the right size, first draw it into a CGImageRef at the needed size once, and reuse it in your drawing code. 这篇关于CGContext的静态背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 13:21