Objective c CAGradientLayer带圆角顶角(不使用.mask)

Objective c CAGradientLayer带圆角顶角(不使用.mask),objective-c,screenshot,calayer,quartz-core,Objective C,Screenshot,Calayer,Quartz Core,我希望创建一个圆角的CAGradientLayer。我不能使用.cornerRadius,因为我只想将顶部的两个角圆角。另外,我不能使用.mask,因为我想在事后拍摄它的屏幕截图,而RenderContext不支持CALayer掩码 我怎样才能: 答:不使用遮罩创建具有渐变和两个圆角的CALayer 或 B:从CurrentImageContext中截取一个类似于UIGraphicsGetImageFromCurrentImageContext的屏幕截图,但要注意遮罩。 我想出了一个程序化的方法

我希望创建一个圆角的CAGradientLayer。我不能使用.cornerRadius,因为我只想将顶部的两个角圆角。另外,我不能使用.mask,因为我想在事后拍摄它的屏幕截图,而RenderContext不支持CALayer掩码

我怎样才能:

答:不使用遮罩创建具有渐变和两个圆角的CALayer 或

B:从CurrentImageContext中截取一个类似于UIGraphicsGetImageFromCurrentImageContext的屏幕截图,但要注意遮罩。
我想出了一个程序化的方法来做这件事。它包括创建一个圆形层,然后在其顶部绘制一个方形层。你必须在渐变颜色和位置上做一些调整,才能得到好看的东西

注意:鉴于您对口罩的要求,不确定此解决方案是否适用于您

+ (void)makeGradientButton:(UIButton *)button {
    // Create a rounded layer
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = button.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
    gradient.cornerRadius = 8.0f;

    // Now create a square layer that draws over the top
    CAGradientLayer *gradient2 = [CAGradientLayer layer];
    gradient2.frame = CGRectMake(0, 9, button.bounds.size.width, button.bounds.size.height-9);
    gradient2.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor redColor] CGColor], nil];
    gradient2.cornerRadius = 0.0f;

    [gradient setMasksToBounds:YES];
    [button.layer insertSublayer:gradient atIndex:0];
    [button.layer insertSublayer:gradient2 atIndex:1];
    [button setBackgroundColor:[UIColor clearColor]];
}

我认为你应该使图层透明,并将图层内容设置为顶部圆角的图像。我希望通过编程实现。这一定是可能的!我想做同样的事情,你明白了吗?