IOS 创建彩色二维码实例详解
程序员文章站
2024-02-18 20:34:34
ios 创建彩色二维码
因为系统创建的二维码默认都是黑色的,所以突然想改变一下二维码颜色,具体操作有点复杂,而且其中用到了好多c语言的语法,swift不好写,所以默认用了...
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
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!