Charts 快捷键图表到共享表
我通过以下方式在SwiftUI中使用图表: 我只想做一个屏幕特定部分的共享表 图表是这样显示的Charts 快捷键图表到共享表,charts,swiftui,Charts,Swiftui,我通过以下方式在SwiftUI中使用图表: 我只想做一个屏幕特定部分的共享表 图表是这样显示的 Bar(entries: [ BarChartDataEntry(x: 1, y: 1), BarChartDataEntry(x: 2, y: 1), BarChartDataEntry(x: 3, y: 1), BarChartDataEntry(x: 4, y: 1), BarChartDataEntr
Bar(entries: [
BarChartDataEntry(x: 1, y: 1),
BarChartDataEntry(x: 2, y: 1),
BarChartDataEntry(x: 3, y: 1),
BarChartDataEntry(x: 4, y: 1),
BarChartDataEntry(x: 5, y: 1)
])
那么,我如何才能更改为图像以便共享它们呢?我不想分享整个页面,只想分享每个图表
谢谢。这是我的工作。股票是标准的东西。该图表来自上述网站 这非常有帮助: 警告;如果在滚动视图中存在问题
import SwiftUI
import Charts
struct ShareSheetView: View {
@State private var showShareSheet = false
@State var rect:CGRect = .zero
@State var uiImage:UIImage? = nil
@State var items : [Any] = []
var body: some View {
VStack(spacing: 20) {
Button(action: {
self.showShareSheet = true
items.removeAll()
self.uiImage = UIApplication.shared.windows[0].rootViewController?.view!.setImage(rect: self.rect)
items.append(self.uiImage as Any)
}) {
Text("Share Chart").padding()
}
ChartView()
.background(RectSettings(rect: $rect))
.frame(width: 300, height: 300 )
} .sheet(isPresented: $showShareSheet) {
ShareSheet(activityItems: items )
}
}
}
struct ShareSheetView_Previews: PreviewProvider {
static var previews: some View {
ShareSheetView()
}
}
struct ChartView: View {
var body: some View {
VStack {
ReturnBarChart(entries: [
BarChartDataEntry(x: 1, y: 1),
BarChartDataEntry(x: 2, y: 2),
BarChartDataEntry(x: 3, y: 3),
BarChartDataEntry(x: 4, y: 5),
BarChartDataEntry(x: 5, y: -2)
])
}
}
}
struct RectSettings: View {
@Binding var rect: CGRect
var body: some View {
GeometryReader { geo in
self.setView(proxy: geo)
}
}
func setView(proxy: GeometryProxy) -> some View {
DispatchQueue.main.async {
self.rect = proxy.frame(in: .global)
}
return Rectangle().fill(Color.clear)
}
}
extension UIView {
func setImage(rect: CGRect) -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: rect)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
这可能会有帮助,谢谢。我刚刚找到了我发布的答案,但如果有人有更好的方法,我真的很想听到他们