//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄
#import"SHCodeTableViewCell.h"
#import"SHQRCodeView.h"//导入生成二维码封装类名
@interfaceXHCFCodeTableViewCell()
//全局的二维码类名
@property(nonatomic,strong)SHQRCodeView*qrCodeView;
@end
@implementationSHCodeTableViewCell
- (void)viewDidLoad {
[superviewDidLoad];
//调用自定义创建二维码方法\添加二维码URL
[selfsetCreateTwoDimensionalCode:@"http://www.jianshu.com/u/ff32105744ef"];
}
#pragma mark -创建二维码
- (void)setCreateTwoDimensionalCode:(NSString*)stringCode
{
//1.初始化SHQRCodeView方法封装
self.qrCodeView= [[SHQRCodeViewalloc]init];
//2.生成一个CIImage的二维码,并且控制图片的大小
UIImage*qrcode = [self.qrCodeViewcreateNonInterpolatedUIImageFormCIImage:[self.qrCodeViewcreateQRForString:stringCode]withSize:250.0f];
//3.把CIImage二维码 添加到自定义的customQrcode图片中,并且设置颜色
UIImage*customQrcode = [self.qrCodeViewimageBlackToTransparent:qrcodewithRed:60.0fandGreen:74.0fandBlue:89.0f];
//4.用自定义customQrcode显示在界面上
self.imageCode.image= customQrcode;
//5.如果还想加上阴影,就在ImageView的Layer上使用下面代码添加阴影:
//5.1设置阴影的偏移量
self.imageCode.layer.shadowOffset=CGSizeMake(0,2);
//5.2设置阴影的半径
self.imageCode.layer.shadowRadius=2;
//5.3设置阴影的颜色为黑色
self.imageCode.layer.shadowColor= [UIColorblackColor].CGColor;
//5.4设置阴影的不透明度
self.imageCode.layer.shadowOpacity=3;
}
注意:以下的代码是自己封装好的生成二维码,只要把下面的. h和. m 文件复制到项目的类中即可.
如图片:http://img.blog.csdn.net/20170602174259983?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpaHVib2tl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
*************** 自封装的生成二维码类.h文件 *******************
// Created by石虎on 16/5/10.
// Copyright © 2016年shih. All rights reserved.
#import
@interfaceSHQRCodeView :UIView
/**
因为生成的二维码是一个CIImage,我们直接转换成UIImage的话大小不好控制,所以使用下面方法返回需要大小的UIImage:
*/
- (UIImage*)createNonInterpolatedUIImageFormCIImage:(CIImage*)image withSize:(CGFloat) size ;
/**
因为生成的二维码是黑白的,所以还要对二维码进行颜色填充,并转换为透明背景,使用遍历图片像素来更改图片颜色,
*/
- (UIImage*)imageBlackToTransparent:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue;
/**
首先是二维码的生成,使用CIFilter很简单,直接传入生成二维码的字符串即可:
*/
- (CIImage*)createQRForString:(NSString*)qrString;
@end
*************** 自封装的生成二维码类.m文件 *******************
// Created by石虎on 16/5/10.
// Copyright © 2016年shih. All rights reserved.
//
#import"SHQRCodeView.h"
@interfaceSHQRCodeView()
@end
@implementationSHQRCodeView
- (UIImage*)createNonInterpolatedUIImageFormCIImage:(CIImage*)image withSize:(CGFloat) size {
CGRectextent =CGRectIntegral(image.extent);
CGFloatscale =MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
//创建bitmap;
size_twidth =CGRectGetWidth(extent) * scale;
size_theight =CGRectGetHeight(extent) * scale;
CGColorSpaceRefcs =CGColorSpaceCreateDeviceGray();
CGContextRefbitmapRef =CGBitmapContextCreate(nil, width, height,8,0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext*context = [CIContextcontextWithOptions:nil];
CGImageRefbitmapImage = [contextcreateCGImage:imagefromRect:extent];
CGContextSetInterpolationQuality(bitmapRef,kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
//保存bitmap到图片
CGImageRefscaledImage =CGBitmapContextCreateImage(bitmapRef);
// Cleanup
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return[UIImageimageWithCGImage:scaledImage];
}
- (CIImage*)createQRForString:(NSString*)qrString {
//需要将字符串转换为UTF-8编码的NSData对象
NSData*stringData = [qrStringdataUsingEncoding:NSUTF8StringEncoding];
//创建filter
CIFilter*qrFilter = [CIFilterfilterWithName:@"CIQRCodeGenerator"];
//设置内容和纠错级别
[qrFiltersetValue:stringDataforKey:@"inputMessage"];
[qrFiltersetValue:@"M"forKey:@"inputCorrectionLevel"];
// Send the image back
returnqrFilter.outputImage;
}
voidProviderReleaseData (void*info,constvoid*data,size_tsize){
free((void*)data);
}
- (UIImage*)imageBlackToTransparent:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue{
constintimageWidth = image.size.width;
constintimageHeight = image.size.height;
size_tbytesPerRow = imageWidth *4;
uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
// create context
CGColorSpaceRefcolorSpace =CGColorSpaceCreateDeviceRGB();
CGContextRefcontext =CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight,8, bytesPerRow, colorSpace,
kCGBitmapByteOrder32Little|kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context,CGRectMake(0,0, imageWidth, imageHeight), image.CGImage);
//遍历像素
intpixelNum = imageWidth * imageHeight;
uint32_t* pCurPtr = rgbImageBuf;
for(inti =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;
}
}
//输出图片
CGDataProviderRefdataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight,ProviderReleaseData);
CGImageRefimageRef =CGImageCreate(imageWidth, imageHeight,8,32, bytesPerRow, colorSpace,
kCGImageAlphaLast|kCGBitmapByteOrder32Little, dataProvider,
NULL,true,kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
UIImage* resultUIImage = [UIImageimageWithCGImage:imageRef];
//清理空间
CGImageRelease(imageRef);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
returnresultUIImage;
}