Is there a way to generate QR code image on iOS

2020-05-12 00:49发布

Is there a standard way to generate a QR code and attach it to a mail item from iOS client app (no server code)?

标签: ios qr-code
9条回答
SAY GOODBYE
2楼-- · 2020-05-12 01:27

Since iOS 7, you can use a Core Image filter to generate QR images. See the final tip here:

- (CIImage *)createQRForString:(NSString *)qrString {
    NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];

    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [qrFilter setValue:stringData forKey:@"inputMessage"];

    return qrFilter.outputImage;
}
查看更多
啃猪蹄的小仙女
3楼-- · 2020-05-12 01:27

Using Swift 2

import UIKit
import CoreImage

func createQRFromString(str: String) -> CIImage? {
    let stringData = str.dataUsingEncoding(NSUTF8StringEncoding)

    let filter = CIFilter(name: "CIQRCodeGenerator")

    filter?.setValue(stringData, forKey: "inputMessage")

    filter?.setValue("H", forKey: "inputCorrectionLevel")

    return filter?.outputImage
}

if let img = createQRFromString("Hello world program created by someone") {
    let somImage = UIImage(CIImage: img, scale: 1.0, orientation: UIImageOrientation.Down)
}

enter image description here

Swift 3.0

import UIKit
import CoreImage

func createQRFromString(_ str: String) -> CIImage? {
        let stringData = str.data(using: String.Encoding.utf8)

        let filter = CIFilter(name: "CIQRCodeGenerator")

        filter?.setValue(stringData, forKey: "inputMessage")

        filter?.setValue("H", forKey: "inputCorrectionLevel")

        if let img = createQRFromString("Hello world program created by someone") {
            let somImage = UIImage(ciImage: img, scale: 1.0, orientation: UIImageOrientation.down)
        }

        return filter?.outputImage
    }

if let img = createQRFromString("Hello world program created by someone") {
            let somImage = UIImage(ciImage: img, scale: 1.0, orientation: UIImageOrientation.down)
        }

Swift 4.2

private func createQRFromString(str: String) -> CIImage? {
        let stringData = str.data(using: .utf8)

        let filter = CIFilter(name: "CIQRCodeGenerator")
        filter?.setValue(stringData, forKey: "inputMessage")
        filter?.setValue("H", forKey: "inputCorrectionLevel")

        return filter?.outputImage
    }

    var qrCode: UIImage? {
        if let img = createQRFromString(str: "Hello world program created by someone") {
            let someImage = UIImage(
                ciImage: img,
                scale: 1.0,
                orientation: UIImage.Orientation.down
            )
            return someImage
        }

        return nil
    }
查看更多
再贱就再见
4楼-- · 2020-05-12 01:37

For Obj-C version that perfectly works for me, I've mixed answers पवन and Teja Kumar Bethina:

NSString *qrString = @"My string to encode";
NSData *stringData = [qrString dataUsingEncoding: NSUTF8StringEncoding];

CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];

CIImage *qrImage = qrFilter.outputImage;
float scaleX = self.qrImageView.frame.size.width / qrImage.extent.size.width;
float scaleY = self.qrImageView.frame.size.height / qrImage.extent.size.height;

qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(scaleX, scaleY)];

self.qrImageView.image = [UIImage imageWithCIImage:qrImage
                                             scale:[UIScreen mainScreen].scale
                                       orientation:UIImageOrientationUp];
查看更多
啃猪蹄的小仙女
5楼-- · 2020-05-12 01:42

It has been a while since this question was asked and a number of almost perfect answers have been given already. However I had to tweak and combine several answers to get it working perfectly for AppleTV 4K, iPhone X and iPadPro using Xcode 9.2 in 2018. Here's the code if anyone needs it.

@IBOutlet weak var qrCodeBox: UIImageView!

func createQRFromString(_ str: String, size: CGSize) -> UIImage {
    let stringData = str.data(using: .utf8)

  let qrFilter = CIFilter(name: "CIQRCodeGenerator")!
  qrFilter.setValue(stringData, forKey: "inputMessage")
  qrFilter.setValue("H", forKey: "inputCorrectionLevel")

  let minimalQRimage = qrFilter.outputImage!
  // NOTE that a QR code is always square, so minimalQRimage..width === .height
  let minimalSideLength = minimalQRimage.extent.width

  let smallestOutputExtent = (size.width < size.height) ? size.width : size.height
  let scaleFactor = smallestOutputExtent / minimalSideLength
  let scaledImage = minimalQRimage.transformed(
    by: CGAffineTransform(scaleX: scaleFactor, y: scaleFactor))

  return UIImage(ciImage: scaledImage, 
                 scale: UIScreen.main.scale, 
                 orientation: .up)
}

override func viewDidLoad() {
    super.viewDidLoad()
    let myQRimage = createQRFromString("https://www.apple.com", 
                      size: qrCodeBox.frame.size)
    qrCodeBox.image = myQRimage
}
查看更多
走好不送
6楼-- · 2020-05-12 01:42

...which will let me generate links...

First, you need to find a short link service, e.g. bit.ly or goo.by, to make a long link shorter and thus reduce QR Code size.

To perform this task automatically you will have to use some web service.

...then generate a QR code...

ZXing is a popular open source QR Code generator also available for iOS

查看更多
我只想做你的唯一
7楼-- · 2020-05-12 01:44

I created a NSString Category for Obj-C, taking Mike Demidov awesome answer

NString+GGQRCode.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface NSString (GGQRCode)

-(UIImage *)qrCodeImage:(CGFloat)width height:(CGFloat)height;
-(UIImage *)qrCodeImage:(CGFloat)width height:(CGFloat)height scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;

@end

NString+GGQRCode.m

#import "NSString+GGQRCode.h"

@implementation NSString (GGQRCode)

-(UIImage *)qrCodeImage:(CGFloat)width height:(CGFloat)height
{
    return [self qrCodeImage:width height:height scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
}

-(UIImage *)qrCodeImage:(CGFloat)width height:(CGFloat)height scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
{
    NSData *stringData = [self dataUsingEncoding: NSUTF8StringEncoding];

    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];

    CIImage *qrImage = qrFilter.outputImage;
    float scaleX = width / qrImage.extent.size.width;
    float scaleY = height / qrImage.extent.size.height;

    qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(scaleX, scaleY)];

    return [UIImage imageWithCIImage:qrImage scale:scale orientation:orientation];
}

@end
查看更多
登录 后发表回答