如何在iOS中更改SVG图像的颜色?

7uzetpgm  于 2023-11-20  发布在  iOS
关注(0)|答案(7)|浏览(263)

我想改变SVG图像的颜色。我使用下面的代码,它隐藏图像和填充整个图像视图的颜色设置。

self.badmintonImgView.tintColor = [UIColor orangeColor];

字符串
我需要改变图像的颜色。请建议

dsekswqp

dsekswqp1#

如果SVGAssets.xcassets中,你可以告诉Xcode将图像视为图标:
首先,在资产目录中选择SVG映像
然后设置渲染模式为模板图像


的数据

uttx8gqw

uttx8gqw2#

Swift
使用Swift更改SVG的颜色:

badmintonImgView.image = badmintonImgView.image?.withRenderingMode(.alwaysTemplate)
badmintonImgView.tintColor = UIColor.orange

字符串


的数据

bvuwiixz

bvuwiixz3#

这对我很有效:

UIImage * imageOrange = [[UIImage imageNamed:@"yourSVGImageName"] imageWithTintColor:UIColor.orangeColor];
UIImageView * yourImageView = [[UIImageView alloc] initWithImage: imageOrange];

字符串
记住把图片添加到assets文件夹。我希望它能帮助你。

hfwmuf9z

hfwmuf9z4#

在发布iOS 14.0+时,我使用了谷歌orihpt的answer,但我意识到我必须做一些与这里其他答案不同的额外步骤。当使用Image时,我无法让tintColor工作,但foregroundColor对我来说很好。我不知道你是否针对整个图像,但这个解决方案只针对SVG路径,而不是在图像上创建背景颜色(类似于Marcy的output图像)。

Image("imageName")
  .foregroundColor(Color.red)

字符串

ryhaxcpt

ryhaxcpt5#

做下面的事可能对你有用。

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

CALayer* layer = svgImageView.layer;
for (CALayer *subLayer in layer.sublayers) {

    for (CALayer *subSubLayer in subLayer.sublayers) {

        for (CALayer *subSubSubLayer in subSubLayer.sublayers) {

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

字符串

wqsoz72f

wqsoz72f6#

SWIFT 5.X解决方案主要用于从Web动态加载SVG图像:

注意:虽然此解决方案也适用于从Assets文件夹加载的图像和.svg类型以外的图像。

extension UIImage {
/// - Description: returns tinted image
/// - Parameters:
///   - qualityMultiplier: when treating SVG image we need to enlarge the image size in order to preserve quality. The smaller the original SVG is compared to desired UIImage frame, the bigger multiplier should be.
/// - Returns: Tinted image
func withTintColor(_ color: UIColor, qualityMultiplier: CGFloat = 15) -> UIImage? {
    
    UIGraphicsBeginImageContextWithOptions(CGSize(width: size.width * qualityMultiplier, height: size.height * qualityMultiplier), false, scale)
    // 1 We create a rectangle equal to the size of the image
    let drawRect = CGRect(x: 0,y: 0,width: size.width * qualityMultiplier,height: size.height * qualityMultiplier)
    // 2 We set a color and fill the whole space with that color
    color.setFill()
    UIRectFill(drawRect)
    // 3 We draw an image over the space with a blend mode of .destinationIn, which is a mode that treats the image as an image mask
    draw(in: drawRect, blendMode: .destinationIn, alpha: 1)

    let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return tintedImage
}

字符串
}

ffdz8vbo

ffdz8vbo7#

试试这个[self.badmintonImgView setTintColor:[UIColor redColor]];

相关问题