如何在 SwiftUI 中使用 .svg 图像 [英] how to use .svg images in SwiftUI

查看:115
本文介绍了如何在 SwiftUI 中使用 .svg 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以只将 .png 文件上传到资产文件夹中吗?我收到 .svg 文件的错误.我想将它用于 Image() 组件

Can I only upload .png files into the assets folder? I get an error for .svg files. I want to use it for the Image() component

推荐答案

您可以使用 Macaw 来显示 SVG:https://github.com/exyte/Macaw

You can use Macaw to display a SVG: https://github.com/exyte/Macaw

1) 通过 SPM 将 Package 添加到你的项目中(标签 0.9.5 实际上不起作用,你需要使用 master 分支)

1) Add the Package via SPM to your project (the tag 0.9.5 actually doesn't work, you need to use the master branch)

2) 将 .svg 文件移动到项目或在 Assets.xcassets 中创建一个新的数据集,然后将 svg 文件添加到该数据集.

2) Move your .svg-file to the project or create a new Data set in your Assets.xcassets and then add the svg file to this data set.

3) 使用此代码:

struct MyView: View {

    var svgName: String

    var body: some View {
        SVGImage(svgName: self.svgName)
            .frame(width: 50, height: 550)
    }
}

struct SVGImage: UIViewRepresentable {

    var svgName: String

    func makeUIView(context: Context) -> SVGView {
        let svgView = SVGView()
        svgView.backgroundColor = UIColor(white: 1.0, alpha: 0.0)   // otherwise the background is black
        svgView.fileName = self.svgName
        svgView.contentMode = .scaleToFill
        return svgView
    }

    func updateUIView(_ uiView: SVGView, context: Context) {

    }

}

这篇关于如何在 SwiftUI 中使用 .svg 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆