从UIImage转换为SwiftUI图像会产生相同大小的空白图像

问题描述 投票:1回答:1

我正在尝试使用UIImage初始化程序将Image转换为SwiftUI init(uiImage:)。我的UIImage本身是由CIQRCodeGenerator CIFilter生成的CIImage创建的。我正在Xcode 11.1 GM种子1中的Playground上运行我的代码。这是我的全部代码:

import SwiftUI
import UIKit

func qrCodeImage(for string: String) -> Image? {
  let data = string.data(using: String.Encoding.utf8)
  guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
  qrFilter.setValue(data, forKey: "inputMessage")

  guard let ciImage = qrFilter.outputImage else { return nil }
  let uiImage = UIImage(ciImage: ciImage)
  let image = Image(uiImage: uiImage)

  return image
}

let image = qrCodeImage(for: "fdsa")

这是结果:

enter image description here

即使我用CGAffineTransform(scaleX: 10, y: 10)转换图像,最后生成的SwiftUI图像仍然是相同大小,但为空白。

uikit swiftui
1个回答
0
投票

可以使用从数据初始化的Image来确认我在SwiftUI UIImage中遇到相同的问题。可以在调试暂停时验证图像是否已加载,但它不会显示在SwiftUI图像中。

此解决方案对我有用:明确指定图像渲染模式。就我而言,我添加了以下内容:.renderingMode(.original)

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