pptk
V2EX  ›  iOS

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

  •  
  •   pptk · Nov 17, 2020 · 1470 views
    This topic created in 2023 days ago, the information mentioned may be changed or developed.

    有什么解决方法吗

    pptk
        1
    pptk  
    OP
       Nov 17, 2020
    已解决
    //绘制渐变
    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();
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   967 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:57 · PVG 04:57 · LAX 13:57 · JFK 16:57
    ♥ Do have faith in what you're doing.