IOS依据传入字符串生成二维码

h文件

import@interface QrCodeGenerateVC : UIViewController

@end

.m文件

import "QrCodeGenerateVC.h"

@interface QrCodeGenerateVC ()
@property(nonatomic,strong) UIImageView *imageview;
@end
@implementation QrCodeGenerateVC
- (void)viewDidLoad {
[super viewDidLoad];
_imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, SCREENWIDGH, SCREENWIDGH)];

[self.view addSubview:_imageview];

}

 -(void)initErCodeWithString:(NSString *)dataString{

// 1.创建滤镜

CIFilter *filter = [CIFilter   filterWithName:@"CIQRCodeGenerator"];

// 2.还原滤镜默认属性

[filter setDefaults];

// 3.设置需要生成二维码的数据到滤镜中

// OC中要求设置的是一个二进制数据

NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];

[filter setValue:data forKeyPath:@"InputMessage"];

// 4.从滤镜从取出生成好的二维码图片

CIImage *ciImage = [filter outputImage];

self.imageview.layer.shadowOffset = CGSizeMake(0, 0.5);          // 设置阴影的偏移量

self.imageview.layer.shadowRadius = 1;  // 设置阴影的半径

self.imageview.layer.shadowColor = [UIColor     blackColor].CGColor; // 设置阴影的颜色为黑色

self.imageview.layer.shadowOpacity = 0.3; // 设置阴影的不透明度

self.imageview.image = [self     createNonInterpolatedUIImageFormCIImage:ciImage size: SCREENWIDGH-20];

}

//2.生成高清二维码方法

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)ciImage size:(CGFloat)widthAndHeight
{
CGRect extentRect = CGRectIntegral(ciImage.extent);
CGFloat scale = MIN(widthAndHeight / CGRectGetWidth(extentRect), widthAndHeight / CGRectGetHeight(extentRect));
// 1.创建bitmap;
size_t width = CGRectGetWidth(extentRect) * scale;
size_t height = CGRectGetHeight(extentRect) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:ciImage fromRect:extentRect];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extentRect, bitmapImage);
// 保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
//return [UIImage imageWithCGImage:scaledImage]; // 黑白图片
UIImage *newImage = [UIImage imageWithCGImage:scaledImage];
return [self imageBlackToTransparent:newImage withRed:200.0f andGreen:70.0f andBlue:189.0f];
}

//3.设置图片透明度

void ProviderReleaseData (void *info, const void *data, size_t size){

free((void*)data);
}

- (UIImage*)imageBlackToTransparent:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue{

 const int imageWidth = image.size.width;

const int imageHeight = image.size.height;

size_t      bytesPerRow = imageWidth * 4;

uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

 CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,

 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);

CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);

 // 遍历像素

int pixelNum = imageWidth * imageHeight;

uint32_t* pCurPtr = rgbImageBuf;

for (int i = 0; i < pixelNum; i++, pCurPtr++){

if ((*pCurPtr & 0xFFFFFF00) < 0x99999900)    // 将白色变成透明

{

 // 改成下面的代码,会将图片转成想要的颜色

uint8_t* ptr = (uint8_t*)pCurPtr;

 ptr[3] = red; //0~255

  ptr[2] = green;

ptr[1] = blue;

}

else

{

uint8_t* ptr = (uint8_t*)pCurPtr;

   ptr[0] = 0;

   }

}

// 输出图片

CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);

CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace,

kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider,

NULL, true, kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);

UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];

// 清理空间

CGImageRelease(imageRef);

CGContextRelease(context);

 CGColorSpaceRelease(colorSpace);

return resultUIImage;
}

注如果对二维码的清晰度要求不高可使用下面代码

    -(UIImage *)generaterSmallErcode{

 // 1.实例化二维码滤镜

 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

// 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)

 [filter setDefaults];

// 3.将字符串转换成NSdata

 NSData *data  = [@"二维码数据" dataUsingEncoding:NSUTF8StringEncoding];

 // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
[filter setValue:data forKey:@"inputMessage"];

// 5.生成二维码

CIImage *outputImage = [filter outputImage];

UIImage *image = [UIImage imageWithCIImage:outputImage];

 return image;

}

 @end

你可能感兴趣的:(IOS依据传入字符串生成二维码)