LoginSignup
4
0

More than 3 years have passed since last update.

今回、アプリ内でQRコードを生成して表示する必要があったのでRSBarcodesを使いました

Posted at

バーコード生成用クラスを作成

import AVFoundation
import UIKit
import RSBarcodes

/// バーコードを生成する
final class BarcodeGenerator {

    /// 生成予定のコードを検証する
    func validateCode(_ code: String, codeObjectType: AVMetadataObject.ObjectType) -> Bool {

        return RSUnifiedCodeValidator.shared.isValid(
            code,
            machineReadableCodeObjectType: codeObjectType.rawValue
        )
    }
    /// コードを生成する
    func generateCodeImage(_ code: String, codeObjectType: AVMetadataObject.ObjectType) -> UIImage? {

        return RSUnifiedCodeGenerator.shared.generateCode(
            code,
            machineReadableCodeObjectType: codeObjectType.rawValue
        )
    }
    /// スケールを使ってリサイズする
    func resizeCodeImage(_ image: UIImage, scale: CGFloat) -> UIImage? {

        return RSAbstractCodeGenerator.resizeImage(image,
                                                   scale: scale)
    }
    /// サイズを指定してリサイズする
    func resizeCodeImage(_ image: UIImage, size: CGSize, contentMode: UIView.ContentMode) -> UIImage? {

        return RSAbstractCodeGenerator.resizeImage(image,
                                                   targetSize: size,
                                                   contentMode: contentMode)
    }
}

専用のクラスを用意して、ライブラリが持つ機能をメソッド化しました。
import AVFoundationは必須です。

QRコードを生成する場合は、codeObjectType: AVMetadataObject.ObjectType.qrと指定します。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0