how to use SVGKImage change SVG fill color?

北城以北 提交于 2019-12-03 21:52:38

You have to use the CALayer sub-layers to changing the color :

SVGKImage *svgImage = [SVGKImage imageNamed:@"Anchor.svg"];
SVGKLayeredImageView *svgImageView = [[SVGKLayeredImageView alloc] initWithSVGKImage:svgImage];
[capturedImageView addSubview:svgImageView];

CALayer* layer = svgImageView.layer;
for (CALayer *subLayer in layer.sublayers) {
    DLog(@"%@", [subLayer class]);

    for (CALayer *subSubLayer in subLayer.sublayers) {
        DLog(@"%@", [subSubLayer class]);

        for (CALayer *subSubSubLayer in subSubLayer.sublayers) {
            DLog(@"%@", [subSubSubLayer class]);

            if( [subSubSubLayer isKindOfClass:[CAShapeLayer class]]){
                CAShapeLayer* shapeLayer = (CAShapeLayer*)subSubSubLayer;
                shapeLayer.fillColor = [UIColor redColor].CGColor;
            }
        }
    }
}

Hope this helps.

Source : https://github.com/SVGKit/SVGKit/issues/98

I know a method that can change the color of an UIImage. So, we should get the UIImage from SVGKImage firstly.

UIImage *img = [SVGKImage imageNamed:@"imageName"].UIImage;

And then, we can define a method like this:

- (UIImage *)changeTintColorWithImage:(UIImage *)img color:(UIColor *)tintColor {
UIImage *imageIn = img;
CGRect rect = CGRectMake(0, 0, imageIn.size.width, imageIn.size.height);
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageIn.CGImage);
BOOL opaque = alphaInfo == kCGImageAlphaNoneSkipLast
|| alphaInfo == kCGImageAlphaNoneSkipFirst
|| alphaInfo == kCGImageAlphaNone;
UIGraphicsBeginImageContextWithOptions(imageIn.size, opaque, imageIn.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, imageIn.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextClipToMask(context, rect, imageIn.CGImage);
CGContextSetFillColorWithColor(context, tintColor.CGColor);
CGContextFillRect(context, rect);
UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageOut;
}

OK. this method can help us change the color of an image.It mainly use some APIs in CoreGraphics. And I suggest you can create a category with UIImage, it will be convenient to use.

Now you can change the color of png image itself.

Objective C:

    UIImage *img = [UIImage imageNamed:@"imagename"];
    UIImage *tintedImage = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIImageView *imgVw = [[UIImageView alloc] initWithImage:tintedImage];
    imgVw.frame = CGRectMake(0, 20, 100, 100);
    imgVw.tintColor = [UIColor greenColor];
    [self.view addSubview:imgVw];

SWIFT:

let img = UIImage(named: "imagename")
let tintedImage: UIImage? = img?.withRenderingMode(.alwaysTemplate)
let imgVw = UIImageView(image: tintedImage)
imgVw.frame = CGRect(x: 0, y: 20, width: 100, height: 100)
imgVw.tintColor = UIColor.green
view.addSubview(imgVw)

cheers!!!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!