使用核心图形时,UIImageView的动态背景渐变颜色

时间:2018-08-19 04:58:36

标签: ios drawrect

上下文: 我在resizableImageWithCapInsets之后设置了图像。 ImageView是宽度的伸缩视图,ImageView的宽度将在以后更新。 同时,我也希望该ImageView的图像背景色是动态渐变色(关键在渐变的开始和结束时是不确定的,因此需要根据宽度进行更改)

代码:

static CGGradientRef ProgressGradient() {
    UIColor *leftColor = [UIColor greenColor];
    UIColor *rightColor =[UIColor yellowColor];

    NSArray *gradientColors = [NSArray arrayWithObjects:(id)leftColor.CGColor, (id)rightColor.CGColor, nil];

    CFArrayRef colorRefs  = (__bridge CFArrayRef)gradientColors;
    NSUInteger colorCount = [gradientColors count];

    CGFloat delta      = 1.0f / colorCount;
    CGFloat semi_delta = delta / 2.0f;
    CGFloat locations[colorCount];

    for (NSInteger i = 0; i < colorCount; i++)
    {
        locations[i] = delta * i + semi_delta;
    }

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colorRefs, locations);
    CGColorSpaceRelease(colorSpace);
    return gradient;
}

- (UIImage*)progressImageForDraw:(CGRect)gradientRect
                         byImage:(UIImage*)outputImage
     resizableImageWithCapInsets:(UIEdgeInsets)capInsets
                    resizingMode:(UIImageResizingMode)resizingMode
{

    CGRect imageRect = CGRectMake(0, 0, outputImage.size.width, outputImage.size.height);

    // Create gradient
    CGGradientRef gradient = ProgressGradient();

    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, outputImage.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, imageRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    {
        // Apply gradient
        CGContextClipToMask(context, imageRect, outputImage.CGImage);

        CGRect capRect = UIEdgeInsetsInsetRect(gradientRect, capInsets);
        CGContextDrawTiledImage(context, capRect, outputImage.CGImage);

        CGContextDrawLinearGradient(context, gradient, CGPointMake(CGRectGetMinX(gradientRect), CGRectGetMinY(gradientRect)), CGPointMake(CGRectGetMinX(gradientRect) + CGRectGetWidth(gradientRect), CGRectGetMinY(gradientRect)), (kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation));
        CGGradientRelease(gradient);
    }



    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //return gradientImage;

    UIImage *resizeImage = [gradientImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 7) resizingMode:UIImageResizingModeStretch];
    return resizeImage;
}

- (void)drawRect:(CGRect)rect{
    CGRect gradientRect = self.progressImageView.frame;
    UIGraphicsBeginImageContextWithOptions(self.layer.frame.size, NO, self.progressImageView.image.scale);
    [self.progressImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    UIEdgeInsets capInsets = self.progressImageView.image.capInsets;
    UIImageResizingMode resizingMode = self.progressImageView.image.resizingMode;

    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
        UIImage *progressImage = [weakSelf progressImageForDraw:gradientRect
                                                        byImage:outputImage
                                    resizableImageWithCapInsets:capInsets
                                                   resizingMode:resizingMode];
        dispatch_async(dispatch_get_main_queue(), ^(void){
            if (weakSelf) {
                __strong typeof(weakSelf) strongSelf = weakSelf;
                strongSelf->_maskProgressImageView.image = progressImage;
            }
        });
    });
}

结果显示: enter image description here

我不知道代码哪里出了问题。 我只希望绘图图像可以草绘

0 个答案:

没有答案
相关问题