36 lines
1.2 KiB
Swift
36 lines
1.2 KiB
Swift
import SwiftUI
|
|
import WebKit
|
|
|
|
struct SVGImage: UIViewRepresentable {
|
|
let svgName: String
|
|
|
|
func makeUIView(context: Context) -> WKWebView {
|
|
let webView = WKWebView()
|
|
webView.isOpaque = false
|
|
webView.backgroundColor = .clear
|
|
webView.scrollView.isScrollEnabled = false
|
|
|
|
// 尝试从根目录加载SVG文件
|
|
if let url = Bundle.main.url(forResource: svgName, withExtension: "svg") {
|
|
print("SVG URL found: \(url.path)")
|
|
let request = URLRequest(url: url)
|
|
webView.load(request)
|
|
} else {
|
|
print("Error: Failed to find SVG file with name: \(svgName).svg in main bundle")
|
|
// 打印所有可用的SVG文件
|
|
if let resourceURL = Bundle.main.resourceURL,
|
|
let files = try? FileManager.default.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil) {
|
|
let svgFiles = files.filter { $0.pathExtension.lowercased() == "svg" }
|
|
print("Available SVG files: \(svgFiles)")
|
|
}
|
|
}
|
|
|
|
return webView
|
|
}
|
|
|
|
func updateUIView(_ uiView: WKWebView, context: Context) {}
|
|
}
|
|
|
|
// Usage:
|
|
// SVGImage(svgName: "Avatar")
|