Reputation: 33644
I have the following code to draw inner shadows on my UIView drawRect:
//add some inner shadow to the card box
CGContextSaveGState(context);
CGMutablePathRef cardPath = CGPathCreateMutable();
CGPathAddRect(cardPath, NULL, CGRectInset(mainRect, -42, -42));
CGPathAddPath(cardPath, NULL, mainPathRef);
CGPathCloseSubpath(cardPath);
// Add the visible paths as the clipping path to the context
CGContextAddPath(context, mainPathRef);
CGContextClip(context);
// Now setup the shadow properties on the context
UIColor *innerShadowColor = [UIColor colorWithWhite:133/255.f alpha:1.0];
CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 1.0f), 30.0f, [[UIColor orangeColor] CGColor]);
// Now fill the rectangle, so the shadow gets drawn
[innerShadowColor setFill];
CGContextSaveGState(context);
CGContextAddPath(context, cardPath);
CGContextEOFillPath(context);
CGContextRestoreGState(context);
// Release the paths
CGPathRelease(cardPath);
CGContextRestoreGState(context);
CGPathRelease(mainPathRef);
The issue is that this is really slow. Is there any way to speed this code up a bit?
Upvotes: 1
Views: 471
Reputation: 6082
If possible, I would create shadow in image editor, save it as png and then resize and tile as needed.
Another idea is to use shouldRasterize
property of CALayer. It will cache the result of drawing and use it while your view remains unchanged. In this case performance depends on how often you modify your view contents.
Upvotes: 3