Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KFAnimatedImage Shows Blank While Trying To Take A Screenshot #2147

Open
rocxteady opened this issue Sep 23, 2023 · 1 comment
Open

KFAnimatedImage Shows Blank While Trying To Take A Screenshot #2147

rocxteady opened this issue Sep 23, 2023 · 1 comment

Comments

@rocxteady
Copy link

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

//
//  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
sample

@FaisalShabbir007
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants