如何将图像转换为 UIImage?

问题描述 投票:0回答:3

如何将 SwiftUI

Image
转换为
UIImage

let image = Image(systemName: "circle.fill")
let UIImage = image as UIImage
image uiimage swiftui
3个回答
28
投票

没有直接的方法将图像转换为 UIImage。相反,您应该将图像视为视图,并尝试将该视图转换为 UIImage。 Image conforms to View,所以我们已经有了我们需要的View。现在我们只需要将该视图转换为 UIImage。

我们需要 2 个组件来实现这一点。第一个函数将我们的图像/视图更改为 UIView,第二个函数将我们创建的 UIView 更改为 UIImage。

为了更方便,这两个函数都声明为对其适当类型的扩展。

extension View {
// This function changes our View to UIView, then calls another function
// to convert the newly-made UIView to a UIImage.
    public func asUIImage() -> UIImage {
        let controller = UIHostingController(rootView: self)
        
 // Set the background to be transparent incase the image is a PNG, WebP or (Static) GIF
        controller.view.backgroundColor = .clear 
        
        controller.view.frame = CGRect(x: 0, y: CGFloat(Int.max), width: 1, height: 1)
        UIApplication.shared.windows.first!.rootViewController?.view.addSubview(controller.view)
        
        let size = controller.sizeThatFits(in: UIScreen.main.bounds.size)
        controller.view.bounds = CGRect(origin: .zero, size: size)
        controller.view.sizeToFit()
        
// here is the call to the function that converts UIView to UIImage: `.asUIImage()`
        let image = controller.view.asUIImage()
        controller.view.removeFromSuperview()
        return image
    }
}

extension UIView {
// This is the function to convert UIView to UIImage
    public func asUIImage() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
}

如何使用?

let image: Image = Image("MyImageName") // Create an Image anyhow you want
let uiImage: UIImage = image.asUIImage() // Works Perfectly

奖金

正如我所说,我们将图像视为视图。在此过程中,我们不使用Image的任何特定功能,唯一重要的是我们的Image是一个View(符合View协议)。 这意味着使用此方法,您不仅可以将 Image 转换为 UIImage,还可以将任何 View 转换为 UIImage。

var myView: some View {
// create the view here
}
let uiImage = myView.asUIImage() // Works Perfectly

24
投票

SwiftUI 做不到这样的事情,我敢打赌永远也做不到。它违背了整个框架的概念。但是,您可以:

let uiImage = UIImage(systemName: "circle.fill")
let image = Image(uiImage: uiImage)

0
投票

我一直在使用包装器作为解决方法来实现这个

struct AppImage: View {
    private let name: String

    var body: some View {
        Image(name)
    }

    var asUIImage: UIImage? {
        UIImage(named: name)
    }
}

如果你想从网上下载一张图片,你可以使用Kingfisher来达到相同的输出。

import UIKit
import Kingfisher

struct AppImage: View {
    private let urlString: String

    var body: some View {
        KFImage(URL(string: urlString))
    }

    var asUIImage: UIImage {
        let imageView = UIImageView()
        imageView.kf.setImage(with: URL(string: urlString))
        return imageView.image ?? UIImage(named: "placeholder-image")!
    }
}

稍后,如果您在初始化图像时坚持使用

UIImage
类型,则可以在您的应用程序中使用任何图像作为
AppImage

AppImage(name: "image-name").asUIImage

AppImage(urlString: "https://url-to-your-image.com").asUIImage

© www.soinside.com 2019 - 2024. All rights reserved.