0

如何使用 SVGKImage 更改 SVG 填充颜色?

SVGKImage *svgImage = [SVGKImage imageNamed:@"card.svg"];
svgImage.size = self.bounds.size;

SVGKLayeredImageView *imageLayer = [[SVGKLayeredImageView alloc] initWithSVGKImage:svgImage];
SVGKLayer *layer = (SVGKLayer *)imageLayer.layer;
self.layer.mask = layer;

在这里,我找到了获取 CAShapeLayer 的简单方法

SVGKImage *svgImage = [SVGKImage imageNamed:@"test.svg"]; CAShapeLayer *thisLayer = svgImage.CALayerTree; //thisLayer 在 test.svg 中有 UIBezierPath 信息,所以,你可以用它做任何事情。

4

3 回答 3

3

您必须使用CALayer子图层来更改颜色:

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;
            }
        }
    }
}

希望这可以帮助。

来源:https ://github.com/SVGKit/SVGKit/issues/98

于 2015-06-25T04:24:01.633 回答
1

我知道一种可以改变 UIImage 颜色的方法。所以,我们应该首先从 SVGKImage 中获取 UIImage。

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

然后,我们可以定义一个这样的方法:

- (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;
}

好的。这个方法可以帮助我们改变图像的颜色。它主要使用CoreGraphics中的一些API。并且我建议你可以用 UIImage 创建一个类别,这样使用起来会很方便。

于 2017-03-04T04:04:31.383 回答
0

现在您可以更改 png 图像本身的颜色。

目标 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];

迅速:

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)

干杯!!!

于 2017-09-06T09:01:51.607 回答