加速多次使用“CGContextDrawRadialGradient”的最佳方法在抽屉里?
我无法发布图像,但我使用“CGContextDrawRadialGradient”方法绘制一个阴影蓝色球(直径约 40 像素),它是阴影,并在球周围制作一个“脉冲”白色环(内部和外部渐变在球上)戒指)。环从蓝色球的边缘开始向外扩展(半径随着计时器而增长)。白色的圆环像无线电波一样向外扩张,逐渐消失。
在模拟器中运行看起来很棒,但在 iPhone 4 上运行速度非常慢。铃声应该会在大约一秒内发出脉冲(与在模拟器中一样),但在手机上则需要 15-20 秒。我已经阅读了一些有关 CALayer、CGLayer 的内容,并阅读了一些渐变动画的一些片段,但不清楚我应该使用什么来获得最佳性能。
我该如何加快速度。我应该将球放在一层上,将膨胀环放在另一层上吗?如果是这样,我如何知道要更新绘图矩形上的哪一层?
感谢任何指导。谢谢。
I couldn't post the image, but I use the "CGContextDrawRadialGradient" method to draw a shaded blue ball (~40 pixel diameter), it's shadow and to make a "pulsing" white ring around the ball (inner and outer gradients on the ring). The ring starts at the edges of the blue ball and expands outward (radius grows with a timer). The white ring fades as it expands outward like a radio wave.
Looks great running in the simulator but runs incredibly slow on the iPhone 4. The ring should pulse in about a second (as in simulator), but takes 15-20 seconds on the phone. I have been reading a little about CALayer, CGLayer and reading some segments on a some gradient animation, but it isn't clear what I should be using for best performance.
How do I speed this up. Should I put the ball on a layer and the expanding ring on another layer? If so, how do I know which layer to update on a drawrect?
Appreciate any guidance. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
加快此类速度的唯一方法是预渲染它。确定需要多少图像帧才能使其看起来不错,然后将每个帧绘制到使用 CGBitmapContextCreate 创建的上下文中,并使用 CGBitmapContextCreateImage 捕获图像。可能对图像进行动画处理的最简单方法是设置 UIImageView 的animationImages 属性(尽管还有其他选项,例如 CALayer 动画)。
最新的 Apple 文档最终提到了 iOS 支持哪些像素格式,因此请确保 参考创建位图上下文时的内容。
The only way to speed something like that up is to pre-render it. Determine how many image frames you need to make it look good and then draw each frame into a context you created with CGBitmapContextCreate and capture the image using CGBitmapContextCreateImage. Probably the easiest way to animate the images would be to set the animationImages property of a UIImageView (although there are other options, like CALayer animations).
The newest Apple docs finally mention which pixel formats are supported in iOS so make sure to reference those when creating your bitmap context.