开发者

Best way to speed up multiple uses of "CGContextDrawRadialGradient" in drawrect?

开发者 https://www.devze.com 2023-02-15 05:42 出处:网络
I couldn\'t post the image, but I use the \"CGContextDrawRadialGradient\" method to draw 开发者_StackOverflowa shaded blue ball (~40 pixel diameter), it\'s shadow and to make a \"pulsing\" white ring

I couldn't post the image, but I use the "CGContextDrawRadialGradient" method to draw 开发者_StackOverflowa 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号