美文网首页
固定形状自己填充自定义颜色图

固定形状自己填充自定义颜色图

作者: stevewinds | 来源:发表于2019-12-13 18:14 被阅读0次

    因要根据电量 显示不同的颜色,且图片形状一样,切图多了。太不人性化了
    想到用图像混合,给个底图,想要什么颜色图自己混合颜色。

    BlendMode kCGBlendModeMultiply 说明:正片叠底;混合了前景和背景的颜色,最终颜色比原先的都暗;

    底图白色边框,里面自己用层动态画出来。我们将要渲染的是边框

    简单扩展一下UIImage ,使用起来也简单 

    [self.imageViewBattery setImage:[[UIImage imageNamed:@"battery_back_tintcolour"]imageWithMultiplyTintColor:colorBattery]];

    以下是扩展代码

    #import

    @interface UIImage (Tint)

    - (UIImage *)imageWithMultiplyTintColor:(UIColor *)tintColor;

    @end

    #import "UIImage+Tint.h"

    @implementation UIImage (Tint)

    - (UIImage*)imageWithTintColor:(UIColor*)tintColor blendMode:(CGBlendMode)blendMode

    {

        //We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.

        UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);

        [tintColor setFill];

        CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);

        UIRectFill(bounds);

        //Draw the tinted image in context

        [self drawInRect:boundsblendMode:blendModealpha:1.0f];

        if (blendMode != kCGBlendModeDestinationIn) {

            [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];

        }

        UIImage *tintedImage = UIGraphics GetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        returntintedImage;

    }

    -(UIImage * )imageWithMultiplyTintColor:(UIColor *)tintColor

    {

        return [self imageWithTintColor:tintColor blendMode:kCGBlendModeMultiply];

    }

    @end

    需要注意的是底图非填充部分采用透明色

    相关文章

      网友评论

          本文标题:固定形状自己填充自定义颜色图

          本文链接:https://www.haomeiwen.com/subject/yixqnctx.html