二維碼在iOS中的創(chuàng)建已經(jīng)非常普遍,但是算作偏門(mén)的冷知識(shí),建議不用死記代碼,很頭疼的代碼!建議用的時(shí)候直接在網(wǎng)上找就可以了,今天提供二維碼創(chuàng)建的方法類(lèi),節(jié)省了在網(wǎng)上找的時(shí)間哦!
代碼如下
.h文件中聲明調(diào)用方法和參數(shù)類(lèi)型:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface GJProduceTwoCode : NSObject
/**
創(chuàng)建二維碼
*參數(shù)1:二維碼目標(biāo)字符串
*參數(shù)2:指定生成的大小size - 建議size的大小傳imageView的大小
*參數(shù)3:添加的logo(可以為nil) - 其中l(wèi)ogo的大小為size的5.0(默
認(rèn), 也可以去修改pro)
*/
+ (UIImage *)codeWidthDataString:(NSString *)TargetString size:(CGFloat)size logo:(NSString *)logoName;
@end
.m文件中具體方法的實(shí)現(xiàn)
#import "GJProduceTwoCode.h"
static CGFloat pro = 5.0;
@implementation GJProduceTwoCode
/**
創(chuàng)建二維碼
參數(shù)1:二維碼目標(biāo)字符串
參數(shù)2:指定生成的大小size - 建議size的大小傳imageView的大小
參數(shù)3:添加的logo(可以為nil) - 其中l(wèi)ogo的大小為size的5.0(默認(rèn), 也可以去修改pro)
*/
+ (UIImage *)codeWidthDataString:(NSString *)TargetString size:(CGFloat)size logo:(NSString *)logoName {
//1、創(chuàng)建過(guò)濾器
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//2、過(guò)濾器恢復(fù)默認(rèn)
[filter setDefaults];
//3、給過(guò)濾器添加數(shù)據(jù)(數(shù)據(jù)要轉(zhuǎn)化成UTF8)
NSData *data = [TargetString dataUsingEncoding:NSUTF8StringEncoding];
//4、通過(guò)KVO設(shè)置濾鏡inputMessage數(shù)據(jù)
[filter setValue:data forKey:@"inputMessage"];
//5、獲取輸出的二維碼
CIImage *outputImage = [filter outputImage];
// 畫(huà)出二維碼
UIImage *targeImage = [self createNonInterpolatedUIimaegFormCIImage:outputImage withSize:size logo:(logoName != nil ? logoName : nil)];
return targeImage;
}
#pragma mark - 根據(jù)參數(shù)畫(huà)出二維碼
+ (UIImage *)createNonInterpolatedUIimaegFormCIImage:(CIImage *)image withSize:(CGFloat)size logo:(NSString *)logoName{
// 獲取圖片的frame值,轉(zhuǎn)換
CGRect extent = CGRectIntegral(image.extent);
// 設(shè)置比例
CGFloat scale = MIN(size / CGRectGetWidth(extent), size / CGRectGetHeight(extent));
//1、創(chuàng)建bitMap
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
// 創(chuàng)建圖形上下文
// 設(shè)置漸變空間
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
// 創(chuàng)建圖形上下文空間
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
//
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
//2、保存bitImage圖片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
UIImage *outputImage = [UIImage imageWithCGImage:scaledImage];
//生成logo
BOOL logo = logoName != nil ? YES : NO;
if (logo) {
UIGraphicsBeginImageContextWithOptions(outputImage.size, NO, [[UIScreen mainScreen] scale]);
[outputImage drawInRect:CGRectMake(0, 0, size, size)];
UIImage *waterImage = [UIImage imageNamed:logoName];
//注意:logo不能太大(最大不能超過(guò)二維碼圖片的30%),否則掃描不出來(lái)
[waterImage drawInRect:CGRectMake((size - size / pro) / 2., (size - size / pro) / 2., size / pro, size / pro)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
outputImage = newImage;
}
return outputImage;
}
@end
代碼中的注釋寫(xiě)的已經(jīng)很清楚了,就不再做過(guò)多的解釋了,給一個(gè)效果圖吧!

Snip20171207_2.png