从图片中一点取色

来源:互联网 发布:linux打开命令行窗口 编辑:程序博客网 时间:2024/04/28 07:57
最近看这个比较多人用到,以前有使用过,现在,粘过来给大伙看看.
注意:UIImageView的大小只能跟图片一样大.要不然取色不对.
  1. - (UIColor *) getPixelColorAtLocation:(CGPoint)point {
  2.         UIColor* color = nil;
  3.         CGImageRef inImage = self.image.CGImage;
  4.         // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
  5.         CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
  6.         if (cgctx == NULL) {
  7.                 return nil; /* error */ 
  8.         }
  9.         
  10.     size_t w = CGImageGetWidth(inImage);
  11.         size_t h = CGImageGetHeight(inImage);
  12.         CGRect rect = {{0,0},{w,h}}; 
  13.         
  14.         // Draw the image to the bitmap context. Once we draw, the memory 
  15.         // allocated for the context for rendering will then contain the 
  16.         // raw image data in the specified color space.
  17.         CGContextDrawImage(cgctx, rect, inImage); 
  18.         
  19.         // Now we can get a pointer to the image data associated with the bitmap
  20.         // context.
  21.         unsigned char* data = CGBitmapContextGetData (cgctx);
  22.         if (data != NULL) {
  23.                 //offset locates the pixel in the data from x,y. 
  24.                 //4 for 4 bytes of data per pixel, w is width of one row of data.
  25.                 int offset = 4*((w*round(point.y))+round(point.x));
  26.                 int alpha =  data[offset]; 
  27.                 int red = data[offset+1]; 
  28.                 int green = data[offset+2]; 
  29.                 int blue = data[offset+3]; 
  30.                 //NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
  31.                 color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
  32.                 
  33.         }
  34.         
  35.         // When finished, release the context
  36.         CGContextRelease(cgctx); 
  37.         // Free image data memory for the context
  38.         if (data) { free(data); }
  39.         return color;
  40. }

  41. - (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {
  42.         
  43.         CGContextRef    context = NULL;
  44.         CGColorSpaceRef colorSpace;
  45.         void *          bitmapData;
  46.         int             bitmapByteCount;
  47.         int             bitmapBytesPerRow;
  48.         
  49.         // Get image width, height. We'll use the entire image.
  50.         size_t pixelsWide = CGImageGetWidth(inImage);
  51.         size_t pixelsHigh = CGImageGetHeight(inImage);
  52.         
  53.         // Declare the number of bytes per row. Each pixel in the bitmap in this
  54.         // example is represented by 4 bytes; 8 bits each of red, green, blue, and
  55.         // alpha.
  56.         bitmapBytesPerRow   = (pixelsWide * 4);
  57.         bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);
  58.         
  59.         // Use the generic RGB color space.
  60.         colorSpace = CGColorSpaceCreateDeviceRGB();

  61.         if (colorSpace == NULL)
  62.         {
  63.                 fprintf(stderr, "Error allocating color space\n");
  64.                 return NULL;
  65.         }
  66.         
  67.         // Allocate memory for image data. This is the destination in memory
  68.         // where any drawing to the bitmap context will be rendered.
  69.         bitmapData = malloc( bitmapByteCount );
  70.         if (bitmapData == NULL) 
  71.         {
  72.                 fprintf (stderr, "Memory not allocated!");
  73.                 CGColorSpaceRelease( colorSpace );
  74.                 return NULL;
  75.         }
  76.         
  77.         // Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
  78.         // per component. Regardless of what the source image format is 
  79.         // (CMYK, Grayscale, and so on) it will be converted over to the format
  80.         // specified here by CGBitmapContextCreate.
  81.         context = CGBitmapContextCreate (bitmapData,
  82.                                                                          pixelsWide,
  83.                                                                          pixelsHigh,
  84.                                                                          8,      // bits per component
  85.                                                                          bitmapBytesPerRow,
  86.                                                                          colorSpace,
  87.                                                                          kCGImageAlphaPremultipliedFirst);
  88.         if (context == NULL)
  89.         {
  90.                 free (bitmapData);
  91.                 fprintf (stderr, "Context not created!");
  92.         }
  93.         
  94.         // Make sure and release colorspace before returning
  95.         CGColorSpaceRelease( colorSpace );
  96.         
  97.         return context;
  98. }
复制代码
原创粉丝点击