我一直试图这样做但从未成功过。我有一个CAShapelayer
,其形状由UIBezierpath
定义。我想将其渲染为视网膜图像,所以当我用它子掩盖UIImageview
时,我会得到正确的比例。我UIImageview
中的图像确实是视网膜图像。
这是我的CAShapelayer
代码
CAShapeLayer*shapeLayer = [CAShapeLayer layer];
shapeLayer.path=forgroungPath.CGPath;
shapeLayer.bounds = pathRect;
CGFloat xRect =(CGPathGetBoundingBox(shapeLayer.path).origin.x)* -1;
CGFloat yRect =(CGPathGetBoundingBox(shapeLayer.path).origin.y)* -1;
[shapeLayer setAffineTransform:CGAffineTransformMakeTranslation(xRect, yRect)];
shapeLayer.position = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMidY(pathRect));
[shapeLayer setFillColor:[UIColor redColor].CGColor];
[shapeLayer fillColor];
我已经尝试过两次
UIGraphicsImageRenderer*renderer = [[UIGraphicsImageRenderer
alloc]initWithBounds:rectForMainview];
UIImage*shapeImage=[renderer imageWithActions:
^(UIGraphicsImageRendererContext*_Nonnull context){
[shapeLayer renderInContext: context.CGContext];
}];
和
UIGraphicsBeginImageContextWithOptions(rectForMainview.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[shapeLayer renderInContext:ctx];
UIImage * shapeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
即使我将0.0作为比例,但shapeImage也不会呈现为视网膜图像。 任何帮助都非常感激。