I’ve a easy solitaire recreation the place I wish to share a screenshot of successful arms by way of the share sheet. I seize the screenshot effective, however after I go to share (primarily testing by way of Gmail), the primary time the share sheet for Gmail pops up, the picture is just not connected. If I shut the Gmail message and take a look at once more, it masses the picture. Is there some kind of delay I must account for within the rendering course of? I’ve learn a bunch of different threads which have every had components of the identical concern I’ve, however nothing conclusive, and I’ve not been capable of repair it. ShareLink seems to be like it could be a very good choice, however I can’t appear to get it to work, because it crashes my preview each time. Right here is my snapshot (screenshot) operate:
extension WinningHandView {
func snapshot(origin: CGPoint = .zero, measurement: CGSize = .zero) -> UIImage {
let controller = UIHostingController(rootView: self)
let view = controller.view
let targetSize = measurement == .zero ? controller.view.intrinsicContentSize : measurement
view?.backgroundColor = .clear
view?.bounds = CGRect(origin: origin, measurement: targetSize)
let renderer = UIGraphicsImageRenderer(measurement: targetSize)
return renderer.picture { _ in
view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
}
}
}
Here is the Button I am utilizing to launch the Share sheet:
Button("Share", motion: {
let picture = self.snapshot()
//let sharingImage = Picture(uiImage: picture).renderingMode(.unique)*/
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
let _: Void? = UIApplication.shared.connectedScenes.map({ $0 as? UIWindowScene }).compactMap({ $0 }).first?.home windows.first?.rootViewController?.current(activityVC, animated: true, completion: nil)})
It looks like some kind of race situation/delay wanted; if that was the case, how would I implement that? I’ve solely been working with SwiftUi for a few weeks, so nonetheless very a lot studying the ropes.