Iphone 如何通过触摸块区域中的任意位置来填充UIImageView中的特定块?

Iphone 如何通过触摸块区域中的任意位置来填充UIImageView中的特定块?,iphone,core-image,Iphone,Core Image,我有一个UIImageView,如下所示。图像中的每个块都有不同的大小。并且没有定义图像的任何绑定区域。我们必须通过触摸块区域中的任何位置来填充特定的块。假设我单击了一个白色块,那么它应该填充特定的颜色。我的问题是,我们如何检测到要填充的块边界填满 我们将得到触摸的位置,但通过比较哪个边界,我可以找出哪个区域需要填充,因为它只是一个图像 最后我解决了我的问题。使用CGContext。我编写了以下代码来满足需求 @implementation FillBoxViewController

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

最后我解决了我的问题。使用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
@实现FillBoxViewController
-(无效)viewDidLoad
{
[_imgViewSizeToFit];
[[u imgView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHight)];
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
CGPoint pt=[触摸位置视图:self.view];
CGContextRef-ctx;
CGImageRef imageRef=[[u imgView.image CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
如果(pt.y

最后我解决了我的问题。使用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
@实现FillBoxViewController
-(无效)viewDidLoad
{
[_imgViewSizeToFit];
[[u imgView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHight)];
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
CGPoint pt=[触摸位置视图:self.view];
CGContextRef-ctx;
CGImageRef imageRef=[[u imgView.image CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
如果(第y部分)