gpt4 book ai didi

ios - 使用 renderInContext : for masks created by cgpaths

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:08 24 4
gpt4 key购买 nike

我最近发现 renderinContext: 方法会忽略任何图层蒙版。我正在 try catch 我的应用程序屏幕的视频,我在其中为蒙面 CALayer 的路径设置动画。

为了录制屏幕,我使用了在线找到的 ScreenCaptureView 类。该 View 每 x 秒拍摄一次其 subview 的“屏幕截图”,并将它们编译成视频。

drawRect方法中,我调用了

[[self.layer presentationLayer] renderInContext:context];

网上看了下,找到了有人的解决方法;但是,它适用于图像蒙版。 (我的掩码是 CGPath)。

-(UIImage*) imageFromView:(UIView*)originalView
{
//Creating a fakeView which is initialized as our original view.
//Actually i render images on a fakeView and take screenshot of this fakeView.
UIView *fakeView = [[UIView alloc] initWithFrame:originalView.frame];

[fakeView.layer setMasksToBounds:originalView.layer.masksToBounds];

//Getting subviews of originalView.
for (UIView *view in originalView.subviews)
{
//Getting screenshot of layer of view.
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imageLayer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//If it is masked view, then get masked image.
if (view.layer.mask)
{
//getting screenshot of masked layer.
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer.mask renderInContext:UIGraphicsGetCurrentContext()];

//PNG Representation is most important. otherwise this masked image will not work.
UIImage *maskedLayerImage=[UIImage imageWithData:UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext())];
UIGraphicsEndImageContext();

//getting image by masking original image.
imageLayer = [self maskImage:imageLayer withMask:maskedLayerImage];
}

//If imageLayer is pointing to a valid object, then setting this image to UIImageView, our UIImageView frame having exactly as view.frame.
if (imageLayer)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
[imageView setImage:imageLayer];
[fakeView addSubview:imageView];
}
}

//At the end, taking screenshot of fakeView. This will get your Original Image.
UIGraphicsBeginImageContext(fakeView.bounds.size);
[fakeView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *previewCapturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return previewCapturedImage;
}
}

//Method is used to get masked image.
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage
{
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);

return [UIImage imageWithCGImage:masked];
}

我可以对来自 cgpaths 的掩码做类似的事情吗?

最佳答案

您可以在 renderInContext: 中使用 CGContextClip() 来剪辑到路径:

- (void)renderInContext:(CGContextRef)context {

// Assign maskPath to whatever path you want to mask to (Here, it's assumed that your layer's mask is a CAShapeLayer)
CGPathPathRef maskPath = [(CAShapeLayer *)self.mask path];
CGContextAddPath(context, maskPath);
CGContextClip(context);

// Do drawing code here
}

关于ios - 使用 renderInContext : for masks created by cgpaths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17079973/

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