如何在UIImage中找到具有指定颜色的框架?

时间:2014-05-21 14:01:26

标签: ios uiimage

我有一个UIImage,我正在寻找一种快速获取UIImage内部指定颜色的帧的方法。我找到了一些方法来获得单个像素的颜色。

-(UIColor*)getRGBAFromImage:(UIImage*)image atx:(int)xp atY:(int)yp
{
    NSMutableArray *resultColor = [NSMutableArray array];

    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGRectGetWidth(iv.frame);
    NSUInteger height = CGRectGetHeight(iv.frame);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    unsigned char *rawData = (unsigned char*) calloc(height * width * 4,
        sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;

    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
        bitsPerComponent, bytesPerRow, colorSpace,
        kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);

    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (bytesPerRow * yp) + xp * bytesPerPixel;
    CGFloat red   = (rawData[byteIndex]     * 1.0) /255.0;
    CGFloat green = (rawData[byteIndex + 1] * 1.0)/255.0 ;
    CGFloat blue  = (rawData[byteIndex + 2] * 1.0)/255.0 ;
    CGFloat alpha = 1;        
    byteIndex += 4;

    UIColor *color = [UIColor colorWithRed:red green:green blue:blue
        alpha:alpha];
    [resultColor addObject:color];
//    NSLog(@"width:%i hight:%i Color:%@",width,height,[color description]);
    free(rawData);

    return color;
}

就像在这张图片中一样:!http://imgur.com/oNvDpww我希望从图像中获取红色矩形的框架。 这个似乎工作。但是需要花费大量时间来浏览所有像素。

1 个答案:

答案 0 :(得分:0)

您正在绘制完整图像,然后在{xp, yp}处拍摄像素。你为什么不画那个像素?

- (UIColor *)getRGBAFromImage:(UIImage *)image atx:(int)xp atY:(int)yp
{
    NSMutableArray *resultColor = [NSMutableArray array];

    CGImageRef imageRef = [image CGImage];

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    unsigned char *rawData = (unsigned char*) calloc(4, sizeof(unsigned char));

    NSUInteger bytesPerPixel = 4;

    NSUInteger bytesPerRow = bytesPerPixel;

    NSUInteger bitsPerComponent = 8;

    CGContextRef context = CGBitmapContextCreate(rawData, 1, 1,

                                                 bitsPerComponent, bytesPerRow, colorSpace,

                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGColorSpaceRelease(colorSpace);



    CGContextDrawImage(context, CGRectMake(xp, yp, 1, 1), imageRef);

    CGContextRelease(context);

    // Now your rawData contains the image data in the RGBA8888 pixel format.

    CGFloat red   = (rawData[0] * 1.0f) / 255.0f;

    CGFloat green = (rawData[1] * 1.0f) / 255.0f;

    CGFloat blue  = (rawData[2] * 1.0f) / 255.0f;

    CGFloat alpha = (rawData[3] * 1.0f) / 255.0f;

    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

    [resultColor addObject:color];

    //    NSLog(@"width:%i hight:%i Color:%@",width,height,[color description]);

    free(rawData);

    return color;
}