IOS 創建彩色二維碼
因為系統創建的二維碼默認都是黑色的,所以突然想改變一下二維碼顏色,具體操作有點復雜,而且其中用到了好多C語言的語法,Swift不好寫,所以默認用了OC。只貼了.m文件的代碼,.h文件就是幾個類函數的聲明。
#import "UIImage+CreateQRCode.h" @implementation UIImage (CreateQRCode) + (UIImage *)createQRCode:(NSString *)string andSize:(CGSize)size andColor:(UIColor *)color { UIImage *qrcode = [self createNonInterpolatedUIImageFormCIImage:[self createQRForString:string] withSize:size]; const CGFloat *_components = CGColorGetComponents(color.CGColor); CGFloat red = _components[0] * 255.f; CGFloat green = _components[1] * 255.f; CGFloat blue = _components[2] * 255.f; return [self imageBlackToTransparent:qrcode withRed:red andGreen:green andBlue:blue]; } + (void)setImageViewShadow:(UIImageView *)view { view.layer.shadowOffset = CGSizeMake(0, 2); view.layer.shadowRadius = 2; view.layer.shadowColor = [UIColor blackColor].CGColor; view.layer.shadowOpacity = 0.5; view.backgroundColor = [UIColor clearColor]; } #pragma mark - 創建灰度圖,只有灰度圖才能改變顏色 + (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGSize)size { CGRect extent = CGRectIntegral(image.extent); CGFloat scale = MIN(size.width/CGRectGetWidth(extent), size.height/CGRectGetHeight(extent)); size_t width = CGRectGetWidth(extent) * scale; size_t height = CGRectGetHeight(extent) * scale; // iOS不支持設備依賴顏色空間或通用顏色空間。iOS應用程序必須使用設備顏色空間 // 設備顏色空間主要用於IOS應用程序,因為其它顏色空間無法在IOS上使用。大多數情況下,Mac OS X應用程序應使用通用顏色空間,而不使用設備顏色空間。 // CGColorSpaceCreateDeviceGray:創建設備依賴灰度顏色空間 // CGColorSpaceCreateDeviceRGB:創建設備依賴RGB顏色空間 // CGColorSpaceCreateDeviceCMYK:創建設備依賴CMYK顏色空間 CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();//這個是改變二維碼顏色的主要屬性,必須是灰度空間,作用是將UIImage轉變成了灰度圖 CGContextRef bitmapRef = CGBitmapContextCreate(NULL, width, height, 8, 0, cs, kCGImageAlphaNone); CIContext * context = [CIContext contextWithOptions:NULL]; CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationHigh); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; } #pragma mark - 創建二維碼的主要代碼 + (CIImage *)createQRForString:(NSString *)qrString { NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding]; CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [qrFilter setValue:stringData forKey:@"inputMessage"]; [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"]; return qrFilter.outputImage; } #pragma mark - 改變二維碼的顏色 void ProviderReleaseData (voidvoid *info, const voidvoid *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; } @end
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!