V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pptk
V2EX  ›  iOS

请问一下有人遇到过 CAGradientLayer 无法截图的问题

  •  
  •   pptk · 2020-11-17 11:32:37 +08:00 · 885 次点击
    这是一个创建于 1258 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有什么解决方法吗

    pptk
        1
    pptk  
    OP
       2020-11-17 12:05:04 +08:00
    已解决
    //绘制渐变
    UIColor *firstColor = [[UIColor alloc]initWithCGColor:(CGColorRef)self.bgLayer.colors.firstObject];
    UIColor *lastColor = [[UIColor alloc]initWithCGColor:(CGColorRef)self.bgLayer.colors.lastObject];
    //创建一个 RGB 的颜色空间
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    //定义渐变颜色数组
    CGFloat colors[] = {
    firstColor.qmui_red, firstColor.qmui_green, firstColor.qmui_blue, 1.00,
    lastColor.qmui_red, lastColor.qmui_green, lastColor.qmui_blue, 1.00
    };
    CGGradientRef _gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
    UIGraphicsBeginImageContextWithOptions(self.preImage.size, NO, self.preImage.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextInspectContext(context);
    CGPoint start = CGPointMake(self.preImage.size.width/2, 0);
    CGPoint end = CGPointMake(self.preImage.size.width/2, self.preImage.size.height);
    CGContextDrawLinearGradient(context, _gradient, start, end, kCGGradientDrawsBeforeStartLocation);

    CGImageRef imageRef = self.preImage.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef,CGRectMakeWithSize(self.preImage.size));
    CGContextDrawImage(context, CGRectMakeWithSize(self.preImage.size), subImageRef);

    UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1257 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 17:52 · PVG 01:52 · LAX 10:52 · JFK 13:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.