V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
pptk
V2EX  ›  iOS

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

  •  
  •   pptk · Nov 17, 2020 · 1400 views
    This topic created in 1992 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   ·   2416 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 03:40 · PVG 11:40 · LAX 20:40 · JFK 23:40
    ♥ Do have faith in what you're doing.