I Have bad performance on using shadow effect

后端 未结 4 1717
时光取名叫无心
时光取名叫无心 2021-02-08 12:33

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 th

相关标签:
4条回答
  • 2021-02-08 13:08

    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.

    0 讨论(0)
  • 2021-02-08 13:12

    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]];
    
    0 讨论(0)
  • 2021-02-08 13:24

    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 讨论(0)
  • 2021-02-08 13:30

    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.

    0 讨论(0)
提交回复
热议问题