【问题标题】:drawRect at double the size两倍大小的drawRect
【发布时间】:2012-11-27 12:18:49
【问题描述】:

如果我覆盖 drawRect 以显示图像并在其上放置动态生成的叠加层(请参阅代码),则每当我放大图像时,由于缩放的结果,它会以非常模糊的方式绘制。

图像由两部分组成,一个是从 png 中提取的图像(其原始大小是所需图像的 2 倍,因此在缩放时应该不会出现问题,但确实会出现问题),另一个是根据 rect 动态生成的大小,所以它也应该适应当前的矩形大小,但它没有。

有什么帮助吗?

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

    CGContextDrawImage(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height), [UIImage imageNamed:@"actionBg.png"].CGImage);

    // generate the overlay
    if ([self isActive] == NO && self.fullDelay != 0) { // TODO: remove fullDelay check!
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
        CGContextRef overlayCtx = UIGraphicsGetCurrentContext();

        int segmentSize = (rect.size.height / [self fullDelay]);

        for (int i=0; i<[self fullDelay]; i++) {
            float alpha = 0.9 - (([self fullDelay] * 0.1) - (i * 0.1));
            [[UIColor colorWithRed:120.0/255.0 green:14.0/255.0 blue:14.0/255.0 alpha:alpha] setFill];

            if (currentDelay > i) {
                CGRect r = CGRectMake(0, i * segmentSize, rect.size.width, segmentSize);
                CGContextFillRect(overlayCtx, r);
            }
            [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.3] setFill];
            CGRect line = CGRectMake(0, (i * segmentSize) + segmentSize - 1 , rect.size.width, 1);
            CGContextFillRect(overlayCtx, line);
        }

        UIImage *overlay = UIGraphicsGetImageFromCurrentImageContext();
        UIImage *overlayMasked = [TDUtilities maskImage:overlay withMask:[UIImage imageNamed:@"actionMask.png"]];

        // prevent the drawings to be flipped
        CGContextTranslateCTM(overlayCtx, 0, rect.size.height);
        CGContextScaleCTM(overlayCtx, 1.0, -1.0);

        CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
        CGContextDrawImage(ctx, rect, overlayMasked.CGImage);
        CGContextSetBlendMode(ctx, kCGBlendModeNormal);

        UIGraphicsEndImageContext();
    }

【问题讨论】:

    标签: iphone ios ipad cocoa-touch


    【解决方案1】:

    问题是您将overlayMasked 绘制为带有CGContextDrawImage 的CGImage,它不知道任何规模。如果您处于双倍比例的情况下,您必须自己手动将大小加倍,或者您应该使用知道比例的 UIImage。

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多