用CIFilter產生QRCode二維碼圖片

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   http   

CIFilter不僅僅可以用來做濾鏡,它還可以用來產生二維碼.

CIFilterEffect.h + CIFilterEffect.m

////  CIFilterEffect.h//  CIFilter////  Created by L.S. on 14-5-9.//  Copyright (c) 2014年 L.S. All rights reserved.//#import <Foundation/Foundation.h>@interface CIFilterEffect : NSObject@property (nonatomic, strong, readonly) UIImage *filterImage;- (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name;@property (nonatomic, strong, readonly) UIImage *QRCodeImage;- (instancetype)initWithQRCodeString:(NSString *)string width:(CGFloat)width;@end
////  CIFilterEffect.m//  CIFilter////  Created by L.S. on 14-5-9.//  Copyright (c) 2014年 L.S. All rights reserved.//#import "CIFilterEffect.h"@interface CIFilterEffect ()@property (nonatomic, strong, readwrite) UIImage *filterImage;@property (nonatomic, strong, readwrite) UIImage *QRCodeImage;@end@implementation CIFilterEffect- (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name{    self = [super init];    if (self)    {        // 將UIImage轉換成CIImage        CIImage *ciImage = [[CIImage alloc] initWithImage:image];                // 建立濾鏡        CIFilter *filter = [CIFilter filterWithName:name                                      keysAndValues:kCIInputImageKey, ciImage, nil];        [filter setDefaults];                // 擷取繪製上下文        CIContext *context = [CIContext contextWithOptions:nil];                // 渲染並輸出CIImage        CIImage *outputImage = [filter outputImage];                // 建立CGImage控制代碼        CGImageRef cgImage = [context createCGImage:outputImage                                           fromRect:[outputImage extent]];                _filterImage = [UIImage imageWithCGImage:cgImage];                // 釋放CGImage控制代碼        CGImageRelease(cgImage);    }    return self;}- (instancetype)initWithQRCodeString:(NSString *)string width:(CGFloat)width{    self = [super init];    if (self)    {        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];                [filter setDefaults];                NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];                [filter setValue:data                  forKey:@"inputMessage"];                CIImage *outputImage = [filter outputImage];                CIContext *context = [CIContext contextWithOptions:nil];        CGImageRef cgImage = [context createCGImage:outputImage                                           fromRect:[outputImage extent]];                UIImage *image = [UIImage imageWithCGImage:cgImage                                             scale:0.1                                       orientation:UIImageOrientationUp];                // 不失真的放大        UIImage *resized = [self resizeImage:image                                 withQuality:kCGInterpolationNone                                        rate:5.0];                // 縮放到固定的寬度(高度與寬度一致)        _QRCodeImage = [self scaleWithFixedWidth:width image:resized];                CGImageRelease(cgImage);    }    return self;}- (UIImage *)scaleWithFixedWidth:(CGFloat)width image:(UIImage *)image{    float newHeight = image.size.height * (width / image.size.width);    CGSize size = CGSizeMake(width, newHeight);    UIGraphicsBeginImageContextWithOptions(size, NO, 0);        CGContextRef context = UIGraphicsGetCurrentContext();        CGContextTranslateCTM(context, 0.0, size.height);    CGContextScaleCTM(context, 1.0, -1.0);        CGContextSetBlendMode(context, kCGBlendModeCopy);    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);        UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        return imageOut;}- (UIImage *)resizeImage:(UIImage *)image             withQuality:(CGInterpolationQuality)quality                    rate:(CGFloat)rate{    UIImage *resized = nil;    CGFloat width = image.size.width * rate;    CGFloat height = image.size.height * rate;        UIGraphicsBeginImageContext(CGSizeMake(width, height));    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetInterpolationQuality(context, quality);    [image drawInRect:CGRectMake(0, 0, width, height)];    resized = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return resized;}@end

看看以下使用方式,一行代碼搞定!

以下幾個二維碼,閑得無聊可以掃一掃......

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.