iOS将CGImage加载到CGLayer中并绘制在顶部

时间:2013-11-08 16:04:34

标签: ios cglayer

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

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

enter image description here

- (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;
}

1 个答案:

答案 0 :(得分: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;
}