【问题标题】:IOS Quartz 2D drawRect and ResizingIOS Quartz 2D drawRect 和调整大小
【发布时间】:2012-06-23 13:05:30
【问题描述】:

我正在使用一个名为 Quarkee 的应用程序将徽标从 SVG 转换为 Quartz 2d 代码,这很有效。唯一的问题是我似乎无法弄清楚如何调整结果大小。如果我设置 UIView 的框架,drawRect 的结果会在框架中保持巨大。我如何让它成为我正在设置的框架的大小?

下面是一个输出的例子。

有人可以帮忙吗?

- (void)drawRect:(CGRect)rect
{

CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorspace_1 = CGColorSpaceCreateDeviceRGB();
CGFloat components_1[] = {0.9961,0.9961,0.9961, 1.0000};
CGColorRef color_1 = CGColorCreate(colorspace_1, components_1);
CGContextSetFillColorWithColor(context,color_1);

CGContextMoveToPoint(context, 0.4960,0.1090);
CGContextAddLineToPoint(context,662.9260,0.1090);
CGContextAddLineToPoint(context,662.9260,227.8780);
CGContextAddLineToPoint(context,0.4960,227.8780);
CGContextAddLineToPoint(context,0.4960,0.1090);
CGContextClosePath(context);


CGContextFillPath(context);

【问题讨论】:

  • 假设我用它的自定义 drawRect 调用我的自定义视图(如上) UICustomView *myView = [[UICustomView alloc] initWithFrame:CGRectMake(200, 70, 80,30)];它的框架是正确的,但是在 drawRect 中绘制的结果对于框架来说太大了。如何使 drawRect 将上下文调整为给定矩形的大小。
  • 我试过 CGContextTranslateCTM(context, rect.size.width, rect.size.height); CGContextScaleCTM(上下文, 1.0, -1.0);例如
  • 还要补充一点,生成的 drawRect 非常大,有许多路径,因此无法更改硬编码坐标。

标签: ios drawrect cgcontext


【解决方案1】:

这里的解决方案是使用 view.transform = CGAffineTransformMakeScale(0.5f, 0.5f);调整视图大小

【讨论】:

    【解决方案2】:

    看看这些值:

    CGContextMoveToPoint(context, 0.4960,0.1090);
    CGContextAddLineToPoint(context,662.9260,0.1090);
    CGContextAddLineToPoint(context,662.9260,227.8780);
    CGContextAddLineToPoint(context,0.4960,227.8780);
    CGContextAddLineToPoint(context,0.4960,0.1090);
    

    当调用drawRect 方法时,您可以使用self.bounds 获取视图的当前矩形并根据它调整这些值。您说您已经从应用程序生成了此代码 - 这些应用程序通常在生成代码时硬编码您绘制的图形的大小,因此您必须查看这些值与您绘制的图像的实际大小之间的关系和根据self.bounds 大小使它们动态化...

    【讨论】:

    • 感谢刻字。在这种情况下,drawRect 将带有数百行代码的整个徽标绘制到上下文中,并且正确,这些点是硬编码的,因此无法将它们重写到提供的矩形的边界。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多