在我目前的项目中,我需要绘制一个复杂的背景作为一些UITableView单元格的背景。由于用于绘制此背景的代码非常长并且在单元格的drawRect:方法中执行时CPU很重,因此我决定仅将其渲染一次到CGLayer,然后将其blit到单元格以提高整体性能。
我用来将背景绘制到CGLayer的代码:
+ (CGLayerRef)standardCellBackgroundLayer
{
static CGLayerRef standardCellBackgroundLayer;
if(standardCellBackgroundLayer == NULL)
{
CGContextRef viewContext = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, PLACES_DEFAULT_CELL_HEIGHT);
standardCellBackgroundLayer = CGLayerCreateWithContext(viewContext, rect.size, NULL);
CGContextRef context = CGLayerGetContext(standardCellBackgroundLayer);
// Setup the paths
CGRect rectForShadowPadding = CGRectInset(rect, (PLACES_DEFAULT_CELL_SHADOW_SIZE / 2) + PLACES_DEFAULT_CELL_SIDE_PADDING, (PLACES_DEFAULT_CELL_SHADOW_SIZE / 2));
CGMutablePathRef path = createPathForRoundedRect(rectForShadowPadding, LIST_ITEM_CORNER_RADIUS);
// Save the graphics context state
CGContextSaveGState(context);
// Draw shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), PLACES_DEFAULT_CELL_SHADOW_SIZE, [Skin shadowColor]);
CGContextAddPath(context, path);
CGContextSetFillColorWithColor(context, [Skin whiteColor]);
CGContextFillPath(context);
// Clip for gradient
CGContextAddPath(context, path);
CGContextClip(context);
// Draw gradient on clipped path
CGPoint startPoint = rectForShadowPadding.origin;
CGPoint endPoint = CGPointMake(rectForShadowPadding.origin.x, CGRectGetMaxY(rectForShadowPadding));
CGContextDrawLinearGradient(context, [Skin listGradient], startPoint, endPoint, 0);
// Restore the graphics state and release everything
CGContextRestoreGState(context);
CGPathRelease(path);
}
return standardCellBackgroundLayer;
}
将图层blit到当前上下文的代码:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawLayerAtPoint(context, CGPointMake(0.0, 0.0), [Skin standardCellBackgroundLayer]);
}
这实际上诀窍相当不错,但我唯一的问题是圆角(检查静态方法)。在屏幕上插入时非常锯齿状。当绘图代码处于其原始位置时,情况并非如此:在drawRect方法中。
如何取回这种反讽?
由于某些原因,以下方法对抗反应没有任何影响:
CGContextSetShouldAntialias(context, YES);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetAllowsAntialiasing(context, YES);
提前致谢!
答案 0 :(得分:1)
您可以使用UIGraphicsBeginImageContextWithOptions
并将比例设置为0.0
来简化此操作。
很抱歉唤醒了一个旧帖子,但我遇到了它,所以其他人也可能。有关详细信息,请参阅UIGraphicsBeginImageContextWithOptions
documentation:
如果指定值
0.0
,则比例因子将设置为比例 设备主屏幕的因素。
基本上意味着如果它是视网膜显示器,它将创建视网膜上下文,这样您可以指定0.0
并将坐标视为点。
答案 1 :(得分:0)
我会回答我自己的问题,因为我不久前想出来了。 你应该做一个视网膜感知的背景。锯齿状仅出现在视网膜设备上。
要解决此问题,您应该使用此辅助方法创建一个视网膜上下文:
// Begin a graphics context for retina or SD
void RetinaAwareUIGraphicsBeginImageContext(CGSize size)
{
static CGFloat scale = -1.0;
if(scale < 0.0)
{
UIScreen *screen = [UIScreen mainScreen];
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)
{
scale = [screen scale]; // Retina
}
else
{
scale = 0.0; // SD
}
}
if(scale > 0.0)
{
UIGraphicsBeginImageContextWithOptions(size, NO, scale);
}
else
{
UIGraphicsBeginImageContext(size);
}
}
然后,在您的绘图方法中,调用上面列出的方法,如下所示:
+ (CGLayerRef)standardCellBackgroundLayer
{
static CGLayerRef standardCellBackgroundLayer;
if(standardCellBackgroundLayer == NULL)
{
RetinaAwareUIGraphicsBeginImageContext(CGSizeMake(320.0, 480.0));
CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, PLACES_DEFAULT_CELL_HEIGHT);
...