0

这就是我为屏蔽图像所做的事情。这工作正常。我的问题是 self.imgView.image 不是屏蔽图像。如何检索屏蔽图像?谢谢。

- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView {

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.imgView.frame;
    maskLayer.path = [clippingPath CGPath];
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor];

    self.imgView.layer.mask = maskLayer;

}
4

2 回答 2

1

您可以使用此方法将 CALayer 转换为 UIImage。它来自https://stackoverflow.com/a/3454613/749786

- (UIImage *)imageFromLayer:(CALayer *)layer
{
  UIGraphicsBeginImageContext([layer frame].size);

  [layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return outputImage;
}
于 2013-08-27T01:34:56.163 回答
0

这是 Swift 5.4 中的方法。

func image(from layer: CALayer?) -> UIImage? {
    UIGraphicsBeginImageContext(layer?.frame.size ?? CGSize.zero)

    if let context = UIGraphicsGetCurrentContext() {
        layer?.render(in: context)
    }
    let outputImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return outputImage
}
于 2021-06-22T20:14:11.243 回答