0

我创建了一个视图,该视图将成为要发送到共享表的快照。不幸的是,我第一次点击分享时得到一个零图像。第二次,快照图像显示正常。

struct AffirmationSharingView: View {

//saving this view to send to shareSheet
var viewToShare: some View {
    ZStack{
        Image("night4")
            .resizable()
        Text("affirmation here")
            .foregroundColor(.white)
    }
}

@State private var showShareSheet = false
@State var myImage: UIImage! = UIImage(named: "test")

var body: some View {
    GeometryReader{gp in
        ZStack{
            Image("night4")
                .resizable()
                .frame(width: gp.size.width*0.9, height: gp.size.height*0.5 , alignment: .top)

            Text("affirmation here")

            Spacer()

            HStack{
                VStack{
                    Image("wigshareIcon")
                        .resizable()
                        .aspectRatio(1, contentMode:.fit)
                        .frame(width: gp.size.width*0.2, height: 60, alignment: .top)

                    Text("Share To Other Social Media")
                }
                .onTapGesture {
                    //save the image/affirmation combo to a UIImage to be sent to the share sheet
                    myImage = viewToShare.snapshot()

                    self.showShareSheet = true
                }
            }
            .padding()
        }
    }
}
.onAppear(perform: getImage)

.sheet(isPresented: $showShareSheet, content: {
        //          let myImage3 = viewToShare.snapshot()

    ShareView(activityItems: ["Rest Rise Grow App!",myImage])    //myImage!])   //[myImage as! Any]) //[data])
}

func getImage(){
    self.myImage = viewToShare.snapshot()
    if self.myImage != nil {
        print("1sharesheet has some value")
        print("1sharesheet equals \(myImage)")
    } else {
        print("Did not set 1screenshot")
    }
}


extension View {
    func snapshot() -> UIImage {
        let controller = UIHostingController(rootView: self)
        let view = controller.view

        let targetSize = controller.view.intrinsicContentSize
        view?.bounds = CGRect(origin: .zero, size: targetSize)
        view?.backgroundColor = .clear

        let renderer = UIGraphicsImageRenderer(size: targetSize)

        return renderer.image { _ in
            view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
        }
    }
}

以下是日志:

2021-12-27 20:20:44.389453-0500 Rest Rise Grow[1672:423870] [Snapshotting] View (0x102168800, _TtGC7SwiftUI14_UIHostingViewGVS_15ModifiedContentGS1_GVS_6ZStackGVS_9TupleViewTGS1_VS_14LinearGradientVS_12_FrameLayout_GS1_VS_5ImageGVS_16_OverlayModifierGVS_10_ShapeViewGVS_13_StrokedShapeVVS_16RoundedRectangle6_Inset_VS_5Color___GVS_19_ConditionalContentVS_4TextS14_____GVS_11_ClipEffectS10___GVS_19_BackgroundModifierS12____) drawing with afterScreenUpdates:YES inside CoreAnimation commit is not supported.
2021-12-27 20:20:45.865260-0500 Rest Rise Grow[1672:424205] Metal API Validation Enabled
sharesheet has some value
sharesheet equals Optional(<UIImage:0x280e48360 anonymous {786, 831}>)
Rest_Rise_Grow/SettingsViewController.swift:833: Fatal error: Unexpectedly found nil while unwrapping an Optional value
2021-12-27 20:20:46.296762-0500 Rest Rise Grow[1672:423870] Rest_Rise_Grow/SettingsViewController.swift:833: Fatal error: Unexpectedly found nil while unwrapping an Optional value
4

0 回答 0