当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS 创建彩色二维码实例详解

IOS 创建彩色二维码实例详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

青野武,豪情 淮山杞子,初中学生评语集锦

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 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网