Kingfisher
Kingfisher copied to clipboard
KFAnimatedImage Shows Blank While Trying To Take A Screenshot
Check List
Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.
- [✓] I have read the wiki page and cheat sheet, but there is no information I need.
- [✓] I have searched in existing issues, but did not find a same one.
- [✓] I want to report a problem instead of asking a question. It'd better to use kingfisher tag in Stack Overflow to ask a question.
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
//
// BiseyImageView.swift
// NavigationStackTryings
//
// Created by Ulaş Sancak on 22.09.2023.
//
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)
}
.onChange(of: save) { newValue in
if newValue {
let image = snapshot(size: .init(width: 200, height: 200))
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
}
}
#Preview {
BiseyImageView()
}
extension View {
func snapshot(size: CGSize) -> UIImage {
let controller = UIHostingController(rootView: self)
let view = controller.view
view?.frame = CGRect(origin: .zero, size: size)
view?.backgroundColor = .clear
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image { _ in
view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
}
}
}
Other Comments
This is the result
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.