Is there a way to generate QR code image on iOS

后端 未结 8 2090
-上瘾入骨i
-上瘾入骨i 2020-12-22 19:59

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

8条回答
  •  情话喂你
    2020-12-22 20:47

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

    NString+GGQRCode.h

    #import 
    #import 
    
    @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
    

提交回复
热议问题