.h文件
#import <UIKit/UIKit.h>
@interface QRCoreViewController : UIViewController
@property (nonatomic, copy) NSString *url;//转为二维码的URL
@end
.m文件
#import "QRCoreViewController.h"
@import AVFoundation;
@import Photos;
@interface QRCoreViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic, strong) AVCaptureSession *session;//管道
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *layer;//在屏幕上输出摄像头图像
@property (nonatomic, strong) UIImage *image;//二维码图片
@end
@implementation QRCoreViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc] init];
[self.view addSubview:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.size.equalTo(200);
}];
CIImage *ciImage = [self getImageByString:_url];
imageView.image = [self excludeFuzzyImageFromCIImage:ciImage size:200];
self.image = imageView.image;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(saveQR)];
[imageView addGestureRecognizer:tap];
UIButton *saveBtn = [[UIButton alloc] init];
saveBtn.titleLabel.font = [UIFont systemFontOfSize:17];
saveBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
[saveBtn setTitle:@"保存二维码" forState:UIControlStateNormal];
[saveBtn setTitleColor:TEXT_COLOR forState:UIControlStateNormal];
[self.view addSubview:saveBtn];
[saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(imageView.mas_bottom).offset(20);
make.centerX.equalTo(self.view);
make.size.equalTo(CGSizeMake(100, 40));
}];
[saveBtn addTarget:self action:@selector(saveQR) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark -- 使用给定的字符串获得CIImage类型的对象
- (CIImage *)getImageByString:(NSString *)dataString
{
//首先判断字符串是否合理!
if (!dataString || dataString == nil || [dataString isEqualToString:@""])
{
return nil;
}
//实例化一个滤镜对象
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//1、设置filter的默认值,防止之前的设置对本次转化有影响
[filter setDefaults];
//2、将传入的字符串转换为NSData
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
//3、将NSData传递给滤镜(通过KVC的方式,设置inputMessage)
[filter setValue:data forKey:@"inputMessage"];
//4、由filter对象输出图像
CIImage *outputImage = [filter outputImage];
//5、返回二维码图像
return outputImage;
}
#pragma mark -- 对图像进行清晰处理
- (UIImage *)excludeFuzzyImageFromCIImage: (CIImage *)image size: (CGFloat)size
{
CGRect extent = CGRectIntegral(image.extent);
//通过比例计算,让最终的图像大小合理(正方形是我们想要的)
CGFloat scale = MIN(size / CGRectGetWidth(extent), size / CGRectGetHeight(extent));
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpace, (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);
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
UIImage *clearImage = [UIImage imageWithCGImage:scaledImage];
//切记ARC模式下是不会对CoreFoundation框架的对象进行自动释放的,所以要我们手动释放
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
CGColorSpaceRelease(colorSpace);
CGImageRelease(scaledImage);
return clearImage;
}
- (void)saveQR {//保存二维码按钮触发
[self loadImageFinished:self.image];
}
// 图片保存到系统相册(无法保存到自定义相册)
- (void)loadImageFinished:(UIImage *)image
{
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
if (error == nil) {
[self.view showMsg:@"二维码保存成功"];
}
}