当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS实现从背景图中取色的代码

iOS实现从背景图中取色的代码

2019年07月24日  | 移动技术网移动技术  | 我要评论
本文实例讲解了ios从背景图中取色的代码,分享给大家供大家参考,具体内容如下 实现代码: void *bitmapdata; //内存空间的指针,该内存空间的大

本文实例讲解了ios从背景图中取色的代码,分享给大家供大家参考,具体内容如下

实现代码:

void *bitmapdata; //内存空间的指针,该内存空间的大小等于图像使用rgb通道所占用的字节数。
 
static cgcontextref creatergbabitmapcontext (cgimageref inimage)
{
  cgcontextref context = null;
  cgcolorspaceref colorspace;
  int bitmapbytecount;
  int bitmapbytesperrow;
 
  size_t pixelswide = cgimagegetwidth(inimage); //获取横向的像素点的个数
  size_t pixelshigh = cgimagegetheight(inimage);
 
  bitmapbytesperrow  = (pixelswide * 4); //每一行的像素点占用的字节数,每个像素点的argb四个通道各占8个bit(0-255)的空间
  bitmapbytecount = (bitmapbytesperrow * pixelshigh); //计算整张图占用的字节数
 
  colorspace = cgcolorspacecreatedevicergb();//创建依赖于设备的rgb通道
  //分配足够容纳图片字节数的内存空间
  bitmapdata = malloc( bitmapbytecount );
  //创建coregraphic的图形上下文,该上下文描述了bitmadata指向的内存空间需要绘制的图像的一些绘制参数
  context = cgbitmapcontextcreate (bitmapdata,
                   pixelswide,
                   pixelshigh,
                   8,
                   bitmapbytesperrow,
                   colorspace,
                   kcgimagealphapremultipliedlast);
  //core foundation中通过含有create、alloc的方法名字创建的指针,需要使用cfrelease()函数释放
  cgcolorspacerelease( colorspace );
  return context;
}
 
// 返回一个指针,该指针指向一个数组,数组中的每四个元素都是图像上的一个像素点的rgba的数值(0-255),用无符号的char是因为它正好的取值范围就是0-255
static unsigned char *requestimagepixeldata(uiimage *inimage)
{
  cgimageref img = [inimage cgimage];
  cgsize size = [inimage size];
  //使用上面的函数创建上下文
  cgcontextref cgctx = creatergbabitmapcontext(img);
  cgrect rect = {{0,0},{size.width, size.height}};
  //将目标图像绘制到指定的上下文,实际为上下文内的bitmapdata。
  cgcontextdrawimage(cgctx, rect, img);
  unsigned char *data = cgbitmapcontextgetdata (cgctx);
  //释放上面的函数创建的上下文
  cgcontextrelease(cgctx);
  return data;
}
 
//设置背景原图片,即取色所用的图片
- (void)setsourceimage:(nsstring *)sourceimage imagewidth:(int)_width imageheight:(int)_height {
  //生成指定大小的背景图
  uiimage *im = [uiimage imagenamed:sourceimage];
  uiimage *newimage;
  uiimageview *view = [[uiimageview alloc] initwithimage:im];
  view.frame = cgrectmake(0, 0, _width, _height);
  uigraphicsbeginimagecontext(cgsizemake(_width, _height)); //size 为cgsize类型,即你所需要的图片尺寸
  [im drawinrect:cgrectmake(0, 0, _width, _height)]; //newimagerect指定了图片绘制区域
  newimage = uigraphicsgetimagefromcurrentimagecontext();
  uigraphicsendimagecontext();
 
  width = newimage.size.width;
  height = newimage.size.height;
  //将解析背景图为像素,供取色用
  imgpixel = requestimagepixeldata(newimage);
}
 
//计算颜色
-(uicolor*)calcolor:(cgpoint)apoint {
  int i = 4 * width * round(apoint.y+imageview.frame.size.height/2) + 4 * round(apoint.x+imageview.frame.size.width/2);
  int _r = (unsigned char)imgpixel[i];
  int _g = (unsigned char)imgpixel[i+1];
  int _b = (unsigned char)imgpixel[i+2];
  nslog(@"(%f,%f)",apoint.x,apoint.y);
  nslog(@"red : %f  green: %f  blue: %f",_r/255.0,_g/255.0,_b/255.0);
  return [uicolor colorwithred:_r/255.0f green:_g/255.0f blue:_b/255.0f alpha:1.0];
}  
 
- (void)changcolor:(uicolor *)color{
  int width_;
  if (![util isipad]) {
    width_ = 30;
  } else {
    width_ = 70;
  }
 
  uigraphicsbeginimagecontext(cgsizemake(width_, width_));
  cgcontextref ctx = uigraphicsgetcurrentcontext();
  cgcontextmovetopoint(ctx, 20, 20);
  cgcontextsetfillcolorwithcolor(ctx, color.cgcolor);
  if (![util isipad]) {
    cgcontextaddarc(ctx, width_/2, width_/2, 14.5, 0, 6.3, 0);
  } else {
    cgcontextaddarc(ctx, width_/2+0.5, width_/2, 31.3, 0, 6.3, 0);
  }
  cgcontextfillpath(ctx);
  self->pickedcolorimageview.image = uigraphicsgetimagefromcurrentimagecontext();
  uigraphicsendimagecontext();
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网