Open rocxteady opened 1 year ago
The issue you're experiencing with the GIF not showing up when taking a screenshot of the SwiftUI view containing a KFAnimatedImage might be related to the way Kingfisher handles animated images. Since the KFAnimatedImage is a wrapper around UIImageView, it might not render properly when taking a screenshot using the method you provided.
To fix this issue, you can try a different approach to take the screenshot. Here's an alternative method that might work better:
import SwiftUI import Kingfisher
struct BiseyImageView: View { @State private var save: Bool = false
var body: some View {
VStack {
Button(save ? "Saved" : "Save") {
save.toggle()
}
KFAnimatedImage(URL(string: "https://sample-videos.com/gif/3.gif"))
.frame(width: 200, height: 200)
.onAppear {
if save {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
let image = snapshot()
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
}
}
}
func snapshot() -> UIImage {
let window = UIApplication.shared.windows.first { $0.isKeyWindow }
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(window?.frame.size ?? .zero, false, scale)
window?.drawHierarchy(in: window?.frame ?? .zero, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? UIImage()
}
}
struct BiseyImageView_Previews: PreviewProvider { static var previews: some View { BiseyImageView() } }
In this updated version, the screenshot is taken by capturing the entire window content rather than just the specific SwiftUI view. This should ensure that the GIF rendered by KFAnimatedImage is also included in the screenshot.
Check List
Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.
Issue Description
What
I am trying to take a screenshot of an SwiftUI view but when I use a gif with KFAnimatedImage it shows blank instead of the gif. KFImage works by the way with the same code. Please check the code below.
Reproduce
Other Comments
This is the result