获取单击图标选颜色

CGContextRef CGBitmapContextCreate (

  void *data,

  size_t width,

  size_t height,

  size_t bitsPerComponent,

  size_t bytesPerRow,

  CGColorSpaceRef colorspace,

  CGBitmapInfo bitmapInfo

  );

参数data指向绘图操作被渲染的内存区域,这个内存区域大小应该为(bytesPerRow*height)个字节。如果对绘制操作被渲染的内存区域并无特别的要求,那么可以传递NULL给参数date。

   参数width代表被渲染内存区域的宽度。

   参数height代表被渲染内存区域的高度。

   参数bitsPerComponent被渲染内存区域中组件在屏幕每个像素点上需要使用的bits位,举例来说,如果使用32-bit像素和RGB颜色格式,那么RGBA颜色格式中每个组件在屏幕每个像素点上需要使用的bits位就为32/4=8。

   参数bytesPerRow代表被渲染内存区域中每行所使用的bytes位数。

   参数colorspace用于被渲染内存区域的“位图上下文”。

   参数bitmapInfo指定被渲染内存区域的“视图”是否包含一个alpha(透视)通道以及每个像素相应的位置,除此之外还可以指定组件式是浮点值还是整数值。

获取图片中单个点的颜色:

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
   UIColor* color = nil;
   CGImageRef inImage = self.image.CGImage;
   // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
   CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
   if (cgctx == NULL) { return nil;  }

size_t w = CGImageGetWidth(inImage);
   size_t h = CGImageGetHeight(inImage);
   CGRect rect = {{0,0},{w,h}};

// Draw the image to the bitmap context. Once we draw, the memory 
   // allocated for the context for rendering will then contain the 
   // raw image data in the specified color space.
   CGContextDrawImage(cgctx, rect, inImage);

// Now we can get a pointer to the image data associated with the bitmap
   // context.
   unsigned char* data = CGBitmapContextGetData (cgctx);
   if (data != NULL) {
       //offset locates the pixel in the data from x,y. 
       //4 for 4 bytes of data per pixel, w is width of one row of data.

@try {

int offset = 4*((w*round(point.y))+round(point.x));//用8或4

NSLog(@"offset: %d", offset);
           int alpha =  data[offset]; 
           int red = data[offset+1]; 
           int green = data[offset+2]; 
           int blue = data[offset+3]; 
           NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
           color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
       }
       @catch (NSException * e) {
           NSLog(@"%@",[e reason]);
       }
       @finally {
       }

}
   // When finished, release the context
   CGContextRelease(cgctx); 
   // Free image data memory for the context
   if (data) { free(data); }

return color;
}

创建取点图片工作域:

- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

CGContextRef    context = NULL;
   CGColorSpaceRef colorSpace;
   void *          bitmapData;
   int             bitmapByteCount;
   int             bitmapBytesPerRow;

// Get image width, height. We‘ll use the entire image.
   size_t pixelsWide = CGImageGetWidth(inImage);
   size_t pixelsHigh = CGImageGetHeight(inImage);

// Declare the number of bytes per row. Each pixel in the bitmap in this
   // example is represented by 4 bytes; 8 bits each of red, green, blue, and
   // alpha.
   bitmapBytesPerRow   = (pixelsWide * 4);
   bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

// Use the generic RGB color space.
   colorSpace = CGColorSpaceCreateDeviceRGB();

if (colorSpace == NULL)
   {
       fprintf(stderr, "Error allocating color space\n");
       return NULL;
   }

// Allocate memory for image data. This is the destination in memory
   // where any drawing to the bitmap context will be rendered.
   bitmapData = malloc( bitmapByteCount );
   if (bitmapData == NULL) 
   {
       fprintf (stderr, "Memory not allocated!");
       CGColorSpaceRelease( colorSpace );
       return NULL;
   }

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
   // per component. Regardless of what the source image format is 
   // (CMYK, Grayscale, and so on) it will be converted over to the format
   // specified here by CGBitmapContextCreate.
   context = CGBitmapContextCreate (bitmapData,
                                                                    pixelsWide,
                                                                    pixelsHigh,
                                                                    8,      // bits per component
                                                                    bitmapBytesPerRow,
                                                                    colorSpace,
                                                                    kCGImageAlphaPremultipliedFirst);
   if (context == NULL)
   {
       free (bitmapData);
       fprintf (stderr, "Context not created!");
   }
   // Make sure and release colorspace before returning
   CGColorSpaceRelease( colorSpace );
   
   return context;

}

//-(UIImage*)ImageUtils_GetImageFromView:(UIView*)view {
//    // start a context with the same size of view
//    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
//   
//    // render the view layer into the context
//    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
//   
//    // store into the UIImage
//    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
//   
//    // close the context
//    UIGraphicsEndImageContext();
//   
//    return img;

//}

时间: 2024-08-24 23:51:31

获取单击图标选颜色的相关文章

山寨酷狗更换壁纸,图标的颜色也跟着换

上一篇写了模仿QQ更换主题的思路跟实现,看到酷狗音乐更换壁纸,图标的颜色也跟着换,这种也相当于更换了一套主题,挺有意思的,现在也山寨一下,写写它是怎样实现的. 从图片中可以看出来,图标更换的颜色,是壁纸的主要颜色(试了一些壁纸,并不是这样的,有些并不是图片的主要颜色,不知是否是过滤掉一些颜色,给它个默认颜色值,也不像是使用Palette里提取的颜色,这里就讲提取主颜色吧),那么第一步,就是要获取一张图片的主要颜色,前面一篇博客,android L Palette 的实现原理有讲到,如何将一张图片

Android----- 改变图标原有颜色 和 搜索框

本博客主要讲以下两点知识点 图标改变颜色:Drawable的变色,让Android也能有iOS那么方便的图片色调转换,就像同一个图标,但是有多个地方使用,并且颜色不一样,就可以用这个方法了. 搜索框: 一般是EditText实现,本文 实现 TextView图片和文字居中,键盘搜索. 来看看效果图: 图标改变颜色:第一个界面的左边(二维码)和右边(更多)两个实现,我放进去的图片是黑色的,显示出来是白色的. 搜索框:第一个界面的图片和文字居中,还可以设置间距,第二个见面搜索设置键盘搜索按钮,点击搜

C# 获取文件图标

今天突然想到一个问题,如何去获取一个文件的关联图标呢?于是就上网搜索了一下.现总结如下: 首先明确问题:获取一个文件的关联图标或者是某个类型文件的显示图标. 在网上搜了一圈,发现方法还是比较多的,但是应用C#进行获取的方法不多.我选择一种用.Net库的方法. 使用的类: System.Drawing.Icon ,位于System.Drawing 命名空间. 具体方法: System.Drawing.Icon 类中的静态方法:public static Icon ExtractAssociated

iOS获取app图标和启动图片名字(AppIcon and LaunchImage's name)

在某种场景下,可能我们需要获取app的图标名称和启动图片的名称.比如说app在前台时,收到了远程通知但是通知栏是不会有通知提醒的,这时我想做个模拟通知提示,需要用到icon名称:再比如在加载某个控制器时,想设置该控制器的背景图片为启动图片,需要用到启动图片名称. 而事实上icon图片放在系统AppIcon文件夹里,启动图片放在系统LaunchImage文件夹里,取这些图片的名称和其他一般资源图片名称不一样. 为了方便举例子,咱们先简单粗暴点 假设当前项目只支持iPhone设备,并且只支持竖屏:而

再谈获取网站图标Icon

上一篇文章讨论了一下获取网站图标方法,是通过从根目录直接获取和html解析结合的方式来获取的,并给出了相应的代码示例.这一篇来讨论一个更现成的方法,这个方法是从360导航的页面发现的,在导航页面中点击添加网址,会弹出一个添加网址的对话框,点击126邮箱,可以看到126邮箱和图标就跑到上面去了.查看一下网络监控,可以看到Request URL是http://cdn.website.h.qhimg.com/index.php?domain=www.126.com,Request Method是GET

仿微信主界面导航栏图标字体颜色的变化

在所有的移动产品中,微信的界面做的很简洁,简单,我对微信主界面影响最深的就是微信底部导航栏的图标,以及字体颜色的变化,一直都想实现以下,今天有空,就大体的模仿者做了一遍. 效果图如下: 分析: 底部主要分为图标的渐变,字体颜色的渐变. 图标的颜色的渐变:主要是通过canvas绘制两个不同的图片,控制其图片的alpha透明度,来达到图标的渐变. 字体颜色:字体颜色就很好说了,Animator动画框架应该很熟悉了,在Animator框架中,有一个TypeEven是来计算十六进制色值的,我们可以通过A

C#获取文件格式图标关联应用程序图标

class SystemIcon { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplay

QT5获取系统文件图标,文件路径

获取系统图标: QFileIconProvider icon_provider; QIcon icon = icon_provider.icon(QFileIconProvider::Folder); 其中可以获取的系统图标有: Constant Value QFileIconProvider::Computer 0 QFileIconProvider::Desktop 1 QFileIconProvider::Trashcan 2 QFileIconProvider::Network 3 QF

根据文件扩展名获取系统图标

1 /// <summary> 2 /// 根据文件后缀名获取系统图标. 3 /// </summary> 4 /// <param name="extension"></param> 5 /// <returns></returns> 6 public static ImageSource GetIconByExtension(string extension) 7 { 8 Icon smallIcon = nu