二维码

1. 导入框架

#import 

2. 根据文本框的字符串绘制二维码图片

// 1. 创建一个滤镜(二维码) 
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 1.1 恢复滤镜默认设置 
[filter setDefaults]; 
// 2. 设置路径输入(NSData) 
NSString *str = self.textField.text;
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data forKey:@"inputMessage"]; 
// 3. 从滤镜里面获取图片数据
CIImage *ciimage = [filter outputImage]; 
UIImage *image = [self createNonInterpolatedUIImageFormCIImage:ciimage withSize:100];
//4.绘制到图片控件上
self.imageView.image = image;

3.以下代码不需要掌握, 只需要会使用即可. 后续如果做到类似功能, 直接拷贝

// 根据CIImage, 和指定大小, 生成UIImage图片
+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size { 
CGRect extent = CGRectIntegral(image.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:nil]; CGImageRef bitmapImage = [context createCGImage:image 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];
}
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;
}

你可能感兴趣的:(二维码)