I’m building news feed list table using UITableView. For each row I’m creating rectangle cell with shadow using QuartzCore layer.shadow
Sample code inside custom UITableViewCell class:
-(void)drawRect:(CGRect)rect
{
UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
bgView.backgroundColor = [UIColor whiteColor];
bgView.layer.shadowColor = [UIColor blackColor].CGColor;
bgView.layer.shadowOffset = CGSizeMake(0, 1);
bgView.layer.shadowOpacity = 0.2;
bgView.layer.shadowRadius = 1;
self.backgroundView = bgView;
[bgView release];
}
When I test app, scroll UITableView, the scrolling performance is bad! If I remove shadow, performance is good!
I need your advices. What kind of optimizations I can do in order to get best performance?
Your problem isn’t Objective-C but the shadow! Since iOS 3.2 you can define a
CGPathReffor the shadow, you should build one which includes just the outline of your view to reduce rendering time and increase performance. You could also have the shadow rasterize to avoid redrawing it all the time (set theshouldRasterizeproperty of your layer toYES. Depending on what you want to do with the layer, this might not be the best looking option. And its also a memory/performance trade off, keep that in mind!).The most easiest way to create the needed shadow path should be via the
UIBezierPathclass which has a lot of useful class methods to build various formedCGPathRefobjects, but depending on the shape of your view, you might have to fall back to build your own path by hand.