视图控件iOS开发

iOS图片取色、在图片上取某点的颜色

2018-12-20  本文已影响45人  QuinceyYang

1、创建bitmap context,且context的尺寸必须与被取色的图片相同

2、将被取色图片绘制到context上

3、调用CGBitmapContextGetData()函数获得位图原数据data

4、元数据data是以一个一个的像素点按照width*height排列的,找到取色点对应的像素(4个字节:alpha,red,green,blue),获得RGB色即可

参考代码如下:

//  UIImage+ARGB.m
//  Create by QuinceyYang
- (CGContextRef)createARGBBitmapContext {

    // Get image width, height
    size_t pixelsWide = CGImageGetWidth(self.CGImage);
    size_t pixelsHigh = CGImageGetHeight(self.CGImage);

    // Declare the number of bytes per row
    NSInteger bitmapBytesPerRow  = (pixelsWide * 4);
    NSInteger bitmapByteCount    = (bitmapBytesPerRow * pixelsHigh);
    
    // Use the generic RGB color space.
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL) {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }

    // Allocate memory for image data
    void *bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }
    
    
    // Create the bitmap context
    CGContextRef context = CGBitmapContextCreate (bitmapData,
                                                  pixelsWide,
                                                  pixelsHigh,
                                                  8,      // bits per component
                                                  bitmapBytesPerRow,
                                                  colorSpace,
                                                  kCGImageAlphaPremultipliedFirst);
    if (context == NULL) {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }
    
    // release colorspace before returning
    CGColorSpaceRelease( colorSpace );
    return context;
}

- (UIColor *)getPixelColorAtPoint:(CGPoint)point
{
    UIColor* color = nil;
    CGImageRef inImage = self.CGImage;
    // Create bitmap context to draw the image into
    CGContextRef cgctx = [self createARGBBitmapContext];
    if (cgctx == NULL) {
        return nil; /* error */
    }

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}};
    
    // Draw the image to the bitmap context
    CGContextDrawImage(cgctx, rect, inImage);
    
    // get image data
    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.
        int offset = 4*((w*round(point.y))+round(point.x));
        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)];
    }
    
    //release the context
    CGContextRelease(cgctx);
    // Free image data
    if (data) {
        free(data);
    }
    return color;
}

End

上一篇下一篇

猜你喜欢

热点阅读