【问题标题】:Avoiding Stretching Stroke when Using CGContextScaleCTM使用 CGContextScaleCTM 时避免拉伸笔画
【发布时间】:2012-08-24 11:43:57
【问题描述】:

我在drawRect 中绘制了一个形状,该形状存储在CGMutablePathRef (shapeMutablePath) 中。每次调用drawRect 时,都会拉伸形状以适应屏幕,并在其周围添加笔划边框。我想知道,如何在不拉伸的情况下绘制笔触边框?即拉伸shapeMutablePath,然后在它周围绘制描边边框,这样每次绘制时它的宽度都相同?我尝试更改比例的顺序以及添加和绘制路径无济于事。

- (void) drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    CGContextSetRGBFillColor(context, 1.0000, 1.0000, 1.0000, 1.0000);
    CGContextSetRGBStrokeColor(context,0.0000,0.0000,0.0000,1.0000);
    CGContextSetLineWidth(context, DialogueTextViewLineWidth);

    CGContextScaleCTM (context, self.frame.size.width / self.shapeMutablePathWidth, self.frame.size.height / self.shapeMutablePathHeight);
    CGContextAddPath(context, self.shapeMutablePath);
    CGContextDrawPath(context, kCGPathFillStroke);
    CGContextRestoreGState(context);    
}

【问题讨论】:

    标签: iphone ios cocoa-touch core-graphics


    【解决方案1】:

    而不是缩放 CTM 并使用原始路径:

    CGContextScaleCTM (context, self.frame.size.width / self.shapeMutablePathWidth, self.frame.size.height / self.shapeMutablePathHeight);
    CGContextAddPath(context, self.shapeMutablePath);
    

    ...创建一个转换后的路径并使用它:

    CGAffineTransform trn = CGAffineTransformMakeScale(self.bounds.size.width / self.shapeMutablePathWidth, self.bounds.size.height / self.shapeMutablePathHeight);
    CGPathRef transformedPath = CGPathCreateCopyByTransformingPath(self.shapeMutablePath, &trn);
    CGContextAddPath(context, transformedPath);
    CGPathRelease(transformedPath);
    

    这将填充和描边相同(缩放)的区域,但变换不会影响描边宽度。

    顺便说一句。您通常会使用边界,而不是框架的大小来计算比例。

    【讨论】:

      最近更新 更多