开发者

I Have bad performance on using shadow effect

I put some image views on scroll view. And when I drag this scroll view, I didn't have any problems.

But after I applied shadow effect to these image views, dragging this scroll view has bad performance.

I used shadowOpacity, shadowRadius and shadowOffset methods.

ex:

[ [ anImageView layer ] shadowOpacity: 1.0 ];

If using shadow effect causes bad performance seriously, I will draw shadow of the images directly.

If there are some tips about this issues, please let me know 开发者_StackOverflowthem.

I want to use shadow effect on iOS programically, because I have the worst drawing skill.

Thank you for your reading.


See CALayer.shouldRasterize (iOS 3.2+, but so is shadowOffset/etc):

When the value of this property is YES, the layer is rendered as a bitmap in its local coordinate space and then composited to the destination with any other content. Shadow effects and any filters in the filters property are rasterized and included in the bitmap.

You probably also want to set rasterizationScale appropriately.


While using the reasterized layer indeed increases the performance you will get better (nicer) results using the shadowpath proerty as @wayne-hartman suggests.

Check http://nachbaur.com/blog/fun-shadow-effects-using-custom-calayer-shadowpaths on how to use the CALayer shadow path.


Whenever you work with shadows its better to use a bezier path as the background. This will help you set the shadowPath, which will drastically improve performance. Rasterize will improve performance, but setShadowPath will improve 5x more than just setting rasterize.

path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) cornerRadius:10];
[self.layer setShadowColor:[UIColor blackColor].CGColor];
[self.layer setShadowOpacity:1.0f];
[self.layer setShadowRadius:10.0f];
[self.layer setShadowPath:[path CGPath]];


I've had exactly the same problem. Drawing the shadow is a fairly costly multi-pass operation, so I can kind of understand it and I think the shadow is drawn continuously as you scroll. The only work-around I've found is to render the shadow manually into an image and display that image behind the images in the scroll. This seems to work well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜