QRCode - Swift 中的一款 QR 码生成器
一个简单的 QR 码图像生成器,用于您的应用程序,用 Swift 5 编写。
更多相关 iOS 控件
您也可能对以下用 Swift 编写的 iOS 控件感兴趣
- SwiftColorWheel - 一个令人愉悦的颜色选择器
- DSWaveformImage - 绘制音频文件的波形图像
安装
- 使用 carthage:
github "dmrschmidt/QRCode", ~> 0.6.0
- 使用 CocoaPods:
pod 'QRCode', '~> 0.6.0'
- 手动: 从仓库检出并构建 QRCode.framework,然后将其添加到您的项目中
- 或者直接将 QRCode 文件夹添加到您的项目中。
用法
您可以从 (NS)Data
、(NS)String
或 (NS)URL
创建一个 QRCode
。
// create a QRCode with all the default values
let qrCodeA = QRCode(data: myData)
let qrCodeB = QRCode(string: "my awesome QR code")
let qrCodeC = QRCode(url: URL(string: "https://example.com"))
要获取创建的 QRCode 的 UIImage
表示形式,只需调用它的 image
方法。
let myImage: UIImage? = try? qrCode.image()
如果您为目标输出图像提供了所需的size
(见下文自定义),当所需图像大小对于提供的数据来说太小,即在缩放过程中需要省略一些像素时,该方法可能会抛出异常。
还有一种可用的替代属性unsafeImage
,在这些情况下它将直接返回nil
。但是,如果您从未指定任何自定义大小,则可以使用unsafeImage
,因为图像将自动选择理想的大小。
要在UIImageView
中显示QRCode
,并且如果您喜欢扩展,您可能想要在您的应用中创建一个类似的扩展
extension UIImageView {
convenience init(qrCode: QRCode) {
self.init(image: qrCode.unsafeImage)
}
}
自定义
可以通过以下方式对QRCode
图像进行自定义
// As an immutable let, by setting all up in the respective constructors.
// This is the recommended approach.
let qrCode = QRCode(string: "my customized QR code",
color: UIColor.red,
backgroundColor: UIColor.green,
imageSize: CGSize(width: 100, height: 100),
scale: 1.0,
inputCorrection: .medium)
// As a mutable var, by setting the individual parameters.
var qrCode = QRCode(string: "my customizable QR code")
qrCode.color = UIColor.red // image foreground (or actual code) color
qrCode.backgroundColor = UIColor.blue // image background color
qrCode.size = CGSize(width: 300, height: 300) // final scaled image size
qrCode.scale = 1.0 // image scaling factor
qrCode.inputCorrection = .quartile // amount of error correction information added
现场演示
SoundCard让您能够发送带有音频信息的明信片。
将用于在SoundCard
发送的明信片上放置一个可扫描的代码,该代码链接到音频信息。
在App Store上查看。