【问题标题】:SwiftUI Chart to ShareSheetSwiftUI 图表到 ShareSheet
【发布时间】:2020-10-20 00:04:32
【问题描述】:

我在 SwiftUI 中通过以下方式使用图表:https://medium.com/@zzzzbh/plotting-in-ios-using-charts-framework-with-swiftui-222034a2bea6

我只想做一个屏幕特定部分的 ShareSheet。

图表是这样显示的。

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)

    ])

那么我怎样才能更改为图片以便分享它们呢?我不想分享整个页面 - 只是每个图表。

谢谢。

【问题讨论】:

标签: charts swiftui


【解决方案1】:

我得到了这个工作。 ShareSheet 是标准的东西。图表来自上述网站。

这很有帮助:https://www.youtube.com/watch?v=lbyRsGCvm-Q

警告;如果在滚动视图中有问题。

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

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2020-01-17
    相关资源
    最近更新 更多