如何将CAShapelayer渲染为视网膜图像?

时间:2017-06-16 22:30:24

标签: ios objective-c uiimageview cgcontext cashapelayer

我一直试图这样做但从未成功过。我有一个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也不会呈现为视网膜图像。 任何帮助都非常感激。

0 个答案:

没有答案