iOS 二维码生成 改变颜色 添加中心图
iOS 二维码生成 改变颜色 添加中心图
#pragma mark - 生成二维码图片 + (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)Imagesize logoImageSize:(CGFloat)waterImagesize{ CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [filter setDefaults]; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data forKey:@"inputMessage"];//通过kvo方式给一个字符串,生成二维码 [filter setValue:@"H" forKey:@"inputCorrectionLevel"];//设置二维码的纠错水平,越高纠错水平越高,可以污损的范围越大 CIImage *outPutImage = [filter outputImage];//拿到二维码图片 return [[self alloc] createNonInterpolatedUIImageFormCIImage:outPutImage withSize:Imagesize waterImageSize:waterImagesize]; } - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size waterImageSize:(CGFloat)waterImagesize{ CGRect extent = CGRectIntegral(image.extent); CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 1.创建bitmap; size_t width = CGRectGetWidth(extent) * scale; size_t height = CGRectGetHeight(extent) * scale; //创建一个DeviceGray颜色空间 CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray(); //CGBitmapContextCreate(void * _Nullable data, size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow, CGColorSpaceRef _Nullable space, uint32_t bitmapInfo) //width:图片宽度像素 //height:图片高度像素 //bitsPerComponent:每个颜色的比特值,例如在rgba-32模式下为8 //bitmapInfo:指定的位图应该包含一个alpha通道。 CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone); CIContext *context = [CIContext contextWithOptions:nil]; //创建CoreGraphics image CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 2.保存bitmap到图片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); //原图 UIImage *outputImage = [UIImage imageWithCGImage:scaledImage]; outputImage = [UIImage specialColorImage:outputImage withRed:102 green:102 blue:102]; //给二维码加 logo 图 UIGraphicsBeginImageContextWithOptions(outputImage.size, NO, [[UIScreen mainScreen] scale]); [outputImage drawInRect:CGRectMake(0,0 , size, size)]; //logo图 UIImage *waterimage = [UIImage imageNamed:@"applogo"]; //把logo图画到生成的二维码图片上,注意尺寸不要太大(最大不超过二维码图片的%30),太大会造成扫不出来 [waterimage drawInRect:CGRectMake((size-waterImagesize)/2.0, (size-waterImagesize)/2.0, waterImagesize, waterImagesize)]; UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newPic; } void ProviderReleaseData (void *info, const void *data, size_t size){ free((void*)data); } //改变二维码颜色 + (UIImage *)specialColorImage:(UIImage *)image withRed:(CGFloat)red green:(CGFloat)green blue:(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); //Create context CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGContextRef contextRef = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpaceRef, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); CGContextDrawImage(contextRef, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage); //Traverse pixe int pixelNum = imageWidth * imageHeight; uint32_t* pCurPtr = rgbImageBuf; for (int i = 0; i < pixelNum; i++, pCurPtr++){ if ((*pCurPtr & 0xFFFFFF00) < 0x99999900){ //Change color 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 dataProviderRef = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight,ProviderReleaseData); CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpaceRef, kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProviderRef, NULL, true, kCGRenderingIntentDefault); CGDataProviderRelease(dataProviderRef); UIImage* img = [UIImage imageWithCGImage:imageRef]; //Release CGImageRelease(imageRef); CGContextRelease(contextRef); CGColorSpaceRelease(colorSpaceRef); return img; }