Turn AsyncImage to UIImage

2.1k Views Asked by At

I got an AsyncImage and want to use UIImageWriteToSavedPhotosAlbum for save my AsyncImage to user library.

I used Paul Hudson course for find what to do but it downloads an empty white picture on library on my iPhone and on simulator.

Here's the code:

    func snapshot() -> UIImage {
        let controller = UIHostingController(rootView: self)
        let view = controller.view

        let targetSize = controller.view.intrinsicContentSize
        view?.bounds = CGRect(origin: .zero, size: targetSize)
        view?.backgroundColor = .blue

        let renderer = UIGraphicsImageRenderer(size: targetSize)

        return renderer.image { _ in
            view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
        }
    }
}`

On the view: 

`Button("Save to image") {
                        let image = AstronomyImageView(astronomy: article) //AsyncImage
                        let uiImage: UIImage = image.snapshot()
                                   UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil)
                               }```
        
0

There are 0 best solutions below