0

我需要将图像加载到 CGLayer 并在其上绘制一些路径。

下面的代码不太有效:它不允许我在图像上绘制路径(路径中有间隙)。请看下图。黑色方块是图像,红色虚线是在其上绘制的路径。您可以看到,如果我只是在图像旁边绘制到视图中,路径会正确显示

在此处输入图像描述

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    float scale = [UIScreen mainScreen].scale;
    CGRect bounds = CGRectMake(0, 0, rect.size.width *scale, rect.size.height *scale);

    if(layer == nil)
    {
        layer = CGLayerCreateWithContext(context, bounds.size, NULL);
        layerContext = CGLayerGetContext(layer);
        CGContextScaleCTM(layerContext, scale, scale);
        viewRect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    }

    CGContextSaveGState(layerContext);
    UIGraphicsBeginImageContext (bounds.size);
    UIImage *image = [UIImage imageNamed:@"testimage.png"];
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

    CGContextTranslateCTM(layerContext, 0, image.size.height);
    CGContextScaleCTM(layerContext, 1.0, -1.0);
    CGContextDrawImage(layerContext, imageRect, image.CGImage);
    UIGraphicsEndImageContext();


    CGContextRestoreGState(layerContext);
    UIBezierPath *bezierPath = path.bezierPath;
    CGContextAddPath(layerContext, bezierPath.CGPath);
    CGContextSetLineWidth(layerContext, path.width);
    CGContextSetStrokeColorWithColor(layerContext, path.color.CGColor);
    CGContextSetLineCap(layerContext, kCGLineCapRound);
    CGContextStrokePath(layerContext);


    CGContextDrawLayerInRect(context, viewRect, layer);


    self.empty = NO;
}
4

1 回答 1

1

解决方案:

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    float scale = [UIScreen mainScreen].scale;
    CGRect bounds = CGRectMake(0, 0, rect.size.width *scale, rect.size.height *scale);

    if(layer == nil)
    {
        layer = CGLayerCreateWithContext(context, bounds.size, NULL);
        layerContext = CGLayerGetContext(layer);
        CGContextScaleCTM(layerContext, scale, scale);
        viewRect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);

        CGContextSaveGState(layerContext);
        UIImage *image = [UIImage imageNamed:@"image.png"];
        UIGraphicsBeginImageContext (image.size);
        CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
        CGContextTranslateCTM(layerContext, 0, image.size.height);
        CGContextScaleCTM(layerContext, 1.0, -1.0);
        CGContextDrawImage(layerContext, imageRect, image.CGImage);
        UIGraphicsEndImageContext();
        CGContextRestoreGState(layerContext);
    }

    UIBezierPath *bezierPath = path.bezierPath;
    CGContextAddPath(layerContext, bezierPath.CGPath);
    CGContextSetLineWidth(layerContext, path.width);
    CGContextSetStrokeColorWithColor(layerContext, path.color.CGColor);
    CGContextSetLineCap(layerContext, kCGLineCapRound);
    CGContextStrokePath(layerContext);

    CGContextDrawLayerInRect(context, viewRect, layer);

    self.empty = NO;
}
于 2013-11-08T21:49:20.080 回答