gpt4 book ai didi

iOS加载CGImage到CGLayer中并在上面绘制

转载 作者:行者123 更新时间:2023-11-29 03:30:28 27 4
gpt4 key购买 nike

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

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

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

最佳答案

解决方法:

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

关于iOS加载CGImage到CGLayer中并在上面绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19863448/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com