Giter Club home page Giter Club logo

qrcode's Introduction

QRCode - A QR Code Generator in Swift

Build Status Carthage compatible Swift Package Manager compatible

A simple QR code image generator to use in your apps, written in Swift 5.

More related iOS Controls

You may also find the following iOS controls written in Swift interesting:

If you really like this library (aka Sponsoring)

I'm doing all this for fun and joy and because I strongly believe in the power of open source. On the off-chance though, that using my library has brought joy to you and you just feel like saying "thank you", I would smile like a 4-year old getting a huge ice cream cone, if you'd support my via one of the sponsoring buttons β˜ΊοΈπŸ’•

If you're feeling in the mood of sending someone else a lovely gesture of appreciation, maybe check out my iOS app πŸ’Œ SoundCard to send them a real postcard with a personal audio message.

Buy Me A Coffee

Installation

  • use SPM: add https://github.com/dmrschmidt/QRCode and set "Up to Next Major" with "1.0.0"
  • use carthage: github "dmrschmidt/QRCode", ~> 1.0.0
  • use cocoapods: pod 'SimpleQRCode', '~> 0.6.0'

Usage

You can create a QRCode from either (NS)Data, (NS)String or (NS)URL:

// 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"))

To get the UIImage representation of your created qrCode, simply call it's image method:

let myImage: UIImage? = try? qrCode.image()

If you provide a desired size for the output image (see Customization below), this method can throw, in case the desired image size is too small for the data being provided, i.e. some pixels would need to be omitted during scaling.

There is an alternative attribute unsafeImage accessible, which will simply return nil in these cases. If you never specify any custom size however, you could use unsafeImage instead, since the image will automatically pick the ideal size.

To show the QRCode in a UIImageView, and if you're a fan of extensions, you may want to consider creating an extension in your app like so:

extension UIImageView {
    convenience init(qrCode: QRCode) {
        self.init(image: qrCode.unsafeImage)
    }    
}

Customization

A QRCode image can be customized in the following ways:

// 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

See it live in action

SoundCard - postcards with sound lets you send real, physical postcards with audio messages. Right from your iOS device.

QRCode is used to place a scannable code, which links to the audio message, on postcards sent by SoundCard - postcards with audio.

Β 

Download SoundCard

Download SoundCard on the App Store.

Β 

Screenshot

qrcode's People

Contributors

dmrschmidt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

qrcode's Issues

Background color is misbehaving in layers on 16.1.1

Hey

This is super weird, but I cannot get the background color to work properly on 16.1.1. It works on 16.1.

If I set the background color to say, red, it will render as pink. It's as if there is a semi-transparent white layer between the background color and the QR code itself. This only seems to affect the rendered image when the image is being used in a CILayer, not when used in a regular UIImageView, which makes it even weirder. It's likely an iOS issue given that it occurred after the above version bump.

Is this something worth looking into for this project, or do we think Apple should fix it?

I tried replacing try? qrCode.image().cgImage with just UImage.init(named: "file").cgImage, which looks perfectly normal, so it only affects the QRCode image, for some reason.

Code for generating the QRCode looks like this:

DispatchQueue.global(qos: .userInteractive).async() { [weak self] in
            
            guard let self = self else {return}
            
            var qrCode = QRCode(string: qrCode)!
            qrCode.color = .white
            qrCode.backgroundColor = NyxColors.secondaryBackgroundColor // This is color hex 1A202B
            qrCode.inputCorrection = .low
            qrCode.size = CGSize.init(width: 456, height: 456)
            
            DispatchQueue.main.async() {
                
                UIView.animate(withDuration: 0.3) {
                    self.qrFeedbackContainer.opacity = 0
                    self.qrCodeLayer.contents = try? qrCode.image().cgImage
                    self.qrCodeLayer.opacity = 1
                }
                
            }
        }

Don't mind the missing anti-aliasing, it's disabled on the Simulator.

iOS 16.1 - this is how it should look
Simulator Screen Shot - iPhone 14 Pro - 2022-11-29 at 15 31 16

iOS 16.1.1 - as you can see here, the background is no longer hex 1A202B, but something way brighter.
IMG_5842

Using Cocoapods

I am using pods in my project, how i can install using pods or manually ? please help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.