如何设置从函数返回的图像?
struct RearKeyCard: View {
var body: some View {
Image(uiImage: generateBarcode(from: "Steve Jobs")!)
.background(/*@START_MENU_TOKEN@*/Color.gray/*@END_MENU_TOKEN@*/)
.font(.custom("Baskerville", size: 15.0))
}
}
func generateBarcode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CICode128BarcodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output)
}
}
return nil
}
答案 0 :(得分:1)
这有效:
struct ContentView: View {
var body: some View {
Image(uiImage: self.generateBarcode(from: "Steve Jobs"))
.background(Color.gray)
.font(.custom("Baskerville", size: 15.0))
.scaleEffect(CGSize(width:3, height: 3))
}
func generateBarcode(from string: String) -> UIImage {
let data = string.data(using: .ascii)
if let filter = CIFilter(name: "CICode128BarcodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
if let output = filter.outputImage {
if let cgimg = CIContext(options: nil).createCGImage(output, from: output.extent) {
return UIImage(cgImage: cgimg)
}
}
}
return UIImage()
}
}