将生成器添加到项目
Add generator to project
我有一个 swift 结构:
import Foundation
import SwiftUI
import CoreImage.CIFilterBuiltins
struct Qrview : View {
let context = CIContext()
let filter = CIFilter.qrCodeGenerator()
var url : String
var body: some View {
Image(uiImage: genearteQrcodeimage(url: url))
}
func genearteQrcodeimage(url : String) -> UIImage {
let data = Data(url.utf8)
filter.setValue(data, forKey: "Inputmessage")
if let qrcodeImage = filter.outputImage {
if let qrcodeImage = context.createCGImage(qrcodeImage, from: qrcodeImage.extent){
return UIImage(cgImage: qrcodeImage)
}
}
return UIImage(systemName: "mark") ?? UIImage()
}
}
和一个视图控制器:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var QRImage: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
根据这个视频
https://www.youtube.com/watch?v=bKOdFVirgU0
它将生成一个二维码图像。
如何将此生成的二维码图像添加到我在故事板中创建的视图 (QRImage)。
您可以使用 UIHostingController 来使用视图。首先,您需要在 viewcontroller 中导入 swiftUI,如下所示:
import UIKit
import SwiftUI
class EventsViewController: UIViewController {
@IBOutlet weak var QRImage: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Example of use
let childView = UIHostingController(rootView: Qrview(url: "www.google.com"))
addChild(childView)
childView.view.frame = QRImage.bounds
QRImage.addSubview(childView.view)
childView.didMove(toParent: self)
}
}
顺便说一句,您需要在 SwiftUI 视图中设置框架和过滤器键。注意键值名称(区分大小写):
filter.setValue(data, forKey: "inputMessage")
import Foundation
import SwiftUI
import CoreImage.CIFilterBuiltins
struct Qrview : View {
let context = CIContext()
let filter = CIFilter.qrCodeGenerator()
var url : String
var body: some View {
Image(uiImage: genearteQrcodeimage(url: url))
.resizable().frame(width: 200, height: 200, alignment: .center)
}
func genearteQrcodeimage(url : String) -> UIImage {
let data = Data(url.utf8)
filter.setValue(data, forKey: "inputMessage")
if let qrcodeImage = filter.outputImage {
if let qrcodeImage = context.createCGImage(qrcodeImage, from: qrcodeImage.extent){
return UIImage(cgImage: qrcodeImage)
}
}
return UIImage(systemName: "mark") ?? UIImage()
}
}
这是输出:
我有一个 swift 结构:
import Foundation
import SwiftUI
import CoreImage.CIFilterBuiltins
struct Qrview : View {
let context = CIContext()
let filter = CIFilter.qrCodeGenerator()
var url : String
var body: some View {
Image(uiImage: genearteQrcodeimage(url: url))
}
func genearteQrcodeimage(url : String) -> UIImage {
let data = Data(url.utf8)
filter.setValue(data, forKey: "Inputmessage")
if let qrcodeImage = filter.outputImage {
if let qrcodeImage = context.createCGImage(qrcodeImage, from: qrcodeImage.extent){
return UIImage(cgImage: qrcodeImage)
}
}
return UIImage(systemName: "mark") ?? UIImage()
}
}
和一个视图控制器:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var QRImage: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
根据这个视频
https://www.youtube.com/watch?v=bKOdFVirgU0
它将生成一个二维码图像。 如何将此生成的二维码图像添加到我在故事板中创建的视图 (QRImage)。
您可以使用 UIHostingController 来使用视图。首先,您需要在 viewcontroller 中导入 swiftUI,如下所示:
import UIKit
import SwiftUI
class EventsViewController: UIViewController {
@IBOutlet weak var QRImage: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Example of use
let childView = UIHostingController(rootView: Qrview(url: "www.google.com"))
addChild(childView)
childView.view.frame = QRImage.bounds
QRImage.addSubview(childView.view)
childView.didMove(toParent: self)
}
}
顺便说一句,您需要在 SwiftUI 视图中设置框架和过滤器键。注意键值名称(区分大小写):
filter.setValue(data, forKey: "inputMessage")
import Foundation
import SwiftUI
import CoreImage.CIFilterBuiltins
struct Qrview : View {
let context = CIContext()
let filter = CIFilter.qrCodeGenerator()
var url : String
var body: some View {
Image(uiImage: genearteQrcodeimage(url: url))
.resizable().frame(width: 200, height: 200, alignment: .center)
}
func genearteQrcodeimage(url : String) -> UIImage {
let data = Data(url.utf8)
filter.setValue(data, forKey: "inputMessage")
if let qrcodeImage = filter.outputImage {
if let qrcodeImage = context.createCGImage(qrcodeImage, from: qrcodeImage.extent){
return UIImage(cgImage: qrcodeImage)
}
}
return UIImage(systemName: "mark") ?? UIImage()
}
}
这是输出: