QRCode - Swift 中的二维码生成器

Build Status Carthage compatible Swift Package Manager compatible

一个简单的二维码图像生成器,用 Swift 5 编写,可在您的应用程序中使用。

更多相关的 iOS 控件

您可能也会对以下用 Swift 编写的 iOS 控件感兴趣

如果您真的很喜欢这个库(即赞助)

我做这一切都是为了乐趣和喜悦,并且我坚信开源的力量。但是,万一使用我的库给您带来了快乐,并且您只想说“谢谢”,如果您通过其中一个赞助按钮支持我,我会像一个 4 岁的孩子得到一个巨大的冰淇淋甜筒一样微笑。☺️💕

如果您想向其他人发送一份可爱的感谢,也许可以看看我的 iOS 应用 💌 SoundCard,给他们寄一张带有个人音频信息的真实明信片。

Buy Me A Coffee

安装

用法

您可以从 (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

Screenshot

查看实际效果

SoundCard - 带有声音的明信片 让您可以发送带有音频信息的真实物理明信片。直接从您的 iOS 设备发送。

QRCode 用于在 SoundCard - 带有声音的明信片 发送的明信片上放置一个可扫描的代码,该代码链接到音频消息。

 

Download SoundCard

在 App Store 下载 SoundCard。

 

Screenshot