1

我有一个 UIImageView 如下所示。图像中的每个块都有不同的大小。并且没有定义图像的任何绑定区域。我们必须通过触摸块区域中的任何位置来填充特定块。假设我点击了一个白色块然后它应该填充特定的颜色。我的问题是我们如何检测要填充的块的边界。我们将获得触摸位置,但通过比较哪个边界,我可以找出要填充的区域,因为它只是一个图像。

在此处输入图像描述

4

1 回答 1

1

在此处输入图像描述

最后我解决了我的问题。使用CGContext。我编写了以下代码来完全满足要求。

    @implementation FillBoxViewController

- (void)viewDidLoad
{
    [_imgView sizeToFit];
    [_imgView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    CGPoint pt=[touch locationInView:self.view];

    CGContextRef ctx;
    CGImageRef imageRef = [_imgView.image CGImage];

    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);

    if (pt.y<height)
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        unsigned char *rawData = malloc(height * width * 4);

        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);

        //GET PIXEL FROM POINT

        int index = 4*(width*(round(pt.y))+(round(pt.x)));

        NSLog(@"index is %i",index);

        int R = rawData[index];
        int G = rawData[index+1];
        int B = rawData[index+2];

        NSLog(@"%d   %d   %d", R, G, B);

        //IF YOU WANT TO ALTER THE PIXELS

        for (int row=pt.y-20; row<=pt.y+20;++row)
        {
            for (int col=pt.x-20; col<=pt.x+20;++col)
            {
                int r,g,b;
                int byteIndex=4*(width*(round(row))+(round(col)));
                r=rawData[byteIndex];
                g=rawData[byteIndex+1];
                b=rawData[byteIndex+2];

                if ((r<R+10&&r>R-10) && (g<G+10&&g>G-10) && (b<B+10&&b>B-10)) {
                    rawData[byteIndex]=(char)(255);
                    rawData[byteIndex+1]=(char)(0);
                    rawData[byteIndex+2]=(char)(0);
                }
            }
        }

        ctx = CGBitmapContextCreate(rawData,
                                    CGImageGetWidth( imageRef ),
                                    CGImageGetHeight( imageRef ),
                                    8,
                                    CGImageGetBytesPerRow( imageRef ),
                                    CGImageGetColorSpace( imageRef ),
                                    kCGImageAlphaPremultipliedLast );

        imageRef = CGBitmapContextCreateImage(ctx);

        UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  

        CGContextRelease(ctx);  

        _imgView.image = rawImage;

        free(rawData);
    }

    NSLog(@"touch detected at location (%f,%f)",pt.x,pt.y);
}

@end

应用条件来检查点的颜色并检查颜色的变化,这将停止填充矩形超出边界。由于图像没有完全固定块中的颜色,这就是图像看起来像这样的原因,我必须使用检查 RGB 范围之间的颜色颜色来自触摸图像。

于 2013-02-28T12:51:00.397 回答