封装成了一个工具类 直接上代码
调用方法:
//根据字符串, 二维码size ,logo图片名称, logosize ,logo 圆角,生成指定二维码的
self.QRCodeIV.image = [XKQRCodeTool createQRCodeImageWithUrlString:departId QRCodeImageSize:self.QRCodeIV.frame.size logoImageName:@"Icon-40" logoImageSize:CGSizeMake(50, 50) logoImageCornerRadius:14];
.h文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreImage/CoreImage.h>
@interface XKQRCodeTool : NSObject
/**
* 根据字符串, 二维码size ,logo图片名称, logosize ,logo 圆角,生成指定二维码的UIImage
*/
+(UIImage *)createQRCodeImageWithUrlString:(NSString *)urlStr QRCodeImageSize:(CGSize )QRSize logoImageName:(NSString *)logoImageName logoImageSize:(CGSize ) logoSize logoImageCornerRadius:(CGFloat )cornerRadius;
/**
* 截图
*
* @param view 需要截取的视图
*
* @return 截图 图片
*/
+ (UIImage *)screenShotFormView:(UIView *)view;
/**
* 读取图片中的二维码
*
* @param image 图片
*
* @return 图片中的二维码数据集合 CIQRCodeFeature对象
*/
+ (NSArray *)readQRCodeFromImage:(UIImage *)image;
@end
.m文件
#import "XKQRCodeTool.h"
@implementation XKQRCodeTool
/**
* 根据字符串生成二维码 CIImage 对象
*
* @param urlString 需要生成二维码的字符串
*
* @return 生成的二维码
*/
+(CIImage *)creatQRcodeWithUrlstring:(NSString *)urlString{
// 1.实例化二维码滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
[filter setDefaults];
// 3.将字符串转换成NSdata
NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding];
// 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
[filter setValue:data forKey:@"inputMessage"];
// 5.生成二维码
CIImage *outputImage = [filter outputImage];
return outputImage;
}
/**
* 改变图片大小 (正方形图片)
*
* @param ciImage 需要改变大小的CIImage 对象的图片
* @param size 图片大小 (正方形图片 只需要一个数)
*
* @return 生成的目标图片
*/
+(UIImage *)changeImageSizeWithCIImage:(CIImage *)ciImage andSize:(CGFloat)size{
CGRect extent = CGRectIntegral(ciImage.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 创建bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}];
CGImageRef bitmapImage = [context createCGImage:ciImage fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
/**
* 根据字符串, 二维码size ,logo图片名称, logosize ,logo 圆角,生成指定二维码的UIImage
*
*
*/
+(UIImage *)createQRCodeImageWithUrlString:(NSString *)urlStr QRCodeImageSize:(CGSize )QRSize logoImageName:(NSString *)logoImageName logoImageSize:(CGSize ) logoSize logoImageCornerRadius:(CGFloat )cornerRadius
{
// 根据字符串生成二维码 CIImage 对象
CIImage *outputImage = [self creatQRcodeWithUrlstring:urlStr];
UIImage *QRImage = [self changeImageSizeWithCIImage:outputImage andSize:QRSize.width]; // 改变二维码的大小
;
UIImageView *imageIV = [[UIImageView alloc]initWithImage:QRImage];
imageIV.frame = CGRectMake(0, 0, QRSize.width, QRSize.height);
if ([logoImageName length] > 0) {
CGFloat logoW = logoSize.width;
CGFloat logoH = logoSize.height;
CGFloat logoX = (imageIV.frame.size.width - logoW )/2;
CGFloat logoY = (imageIV.frame.size.height - logoH)/2;
UIImageView *logoIV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:logoImageName]];
logoIV.frame = CGRectMake(logoX, logoY, logoW, logoH);
logoIV.layer.cornerRadius = cornerRadius;
logoIV.clipsToBounds = YES;
[imageIV addSubview:logoIV];
}
UIImage *img = [self screenShotFormView:imageIV];
return img;
}
/**
* 截图
*
* @param view 需要截取的视图
*
* @return 截图 图片
*/
+ (UIImage *)screenShotFormView:(UIView *)view
{
/* size:图像上下文尺寸相当于新的图片尺寸
opaque:YES :不透明 No :透明
0.0:不需要伸缩
*/
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
#pragma mark - 读取图片中的二维码
/**
* 读取图片中的二维码
*
* @param image 图片
*
* @return 图片中的二维码数据集合 CIQRCodeFeature对象
*/
+ (NSArray *)readQRCodeFromImage:(UIImage *)image{
// 创建一个CIImage对象
CIImage *ciImage = [[CIImage alloc] initWithCGImage:image.CGImage options:nil];
CIContext *context = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}]; // 软件渲染
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];// 二维码识别
// 注意这里的CIDetectorTypeQRCode
NSArray *features = [detector featuresInImage:ciImage];
NSLog(@"features = %@",features); // 识别后的结果集
for (CIQRCodeFeature *feature in features) {
NSLog(@"msg = %@",feature.messageString); // 打印二维码中的信息
}
return features;
}
@end