'The Issue with View to UIImage and AsyncImage in SwiftUI

I am a swift newbie, I recently encountered a problem, please help me.

I am trying to make a social software which has a feature to share a message and generate screenshots, I am currently using a method to be able to share screenshots well, but I found that all AsyncImage are not loading properly in the shared images, while they are fine in the view, I have checked many places, but have not found this solution.

AsyncImage does not load properly in the generated image.

This is the resulting view

extension View {
func asImage3() -> 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)
    }
}

The View to UIImage approach that I use.

My idea is that there is no network activity when the view is converted to uiimage, so it causes all AsyncImage not to load properly.

But I'm not sure if I'm thinking this way correctly and how to fix it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source