Iphone 用颜色填充图像的一部分

Iphone 用颜色填充图像的一部分,iphone,ios,Iphone,Ios,我正在做一个IPhone绘画应用程序。我想使用touchevent绘制图像的特定部分,以查找像素数据,然后使用该像素数据绘制图像的其余部分。使用touchevent,我得到了该部分的像素值: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; startPoint = [[touches anyObject] locati

我正在做一个IPhone绘画应用程序。我想使用touchevent绘制图像的特定部分,以查找像素数据,然后使用该像素数据绘制图像的其余部分。使用touchevent,我得到了该部分的像素值:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    startPoint = [[touches anyObject] locationInView:imageView];

    NSLog(@"the value of the index is %i",index);

    NSString* imageName=[NSString stringWithFormat:@"roof%i", index];

    tempColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:imageName]];
    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;
    NSString *tx = [[NSString alloc]initWithFormat:@"%.0f", lastPoint.x];
    NSString *ty = [[NSString alloc]initWithFormat:@"%.0f", lastPoint.y];

    NSLog(@"the vale of the string is %@  and %@",tx,ty);

    int ix=[tx intValue];
    int iy=[ty intValue];
    int z=1;

    NSLog(@"the vale of the string is %i  and %i and z is %i",ix,iy,z);

    [self getRGBAsFromImage:newImage atX:ix andY:iy count1:1];
}
这里我得到了图像的像素数据:

-(void)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count1:(int)count
{
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];

    // First get the image into your data buffer
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    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);

    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
    for (int ii = 0 ; ii < count ; ++ii)
    {
        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 = (rawData[byteIndex + 3] * 1.0) / 255.0;
        byteIndex += 4;
        NSLog(@"the vale of the rbg of red is %f",red);

        UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
        [result addObject:acolor];
    }

    free(rawData);
    return result;
}
-(void)getRGBAsFromImage:(UIImage*)图像atX:(int)xx安迪:(int)yy计数1:(int)计数
{
NSMUTABLEARRY*result=[NSMUTABLEARRY阵列容量:计数];
//首先将图像放入数据缓冲区
CGImageRef imageRef=[图像CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
无符号字符*rawData=malloc(高度*宽度*4);
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSU整数比特分量=8;
CGContextRef context=CGBitmapContextCreate(原始数据、宽度、高度、,
bitsPerComponent、bytesPerRow、颜色空间、,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),imageRef);
CGContextRelease(上下文);
//现在,您的rawData包含RGBA8888像素格式的图像数据。
int byteIndex=(bytesPerRow*yy)+xx*bytesperpoixel;
对于(int ii=0;ii
使用一个公差值,我得到数据。在这里,我正在努力画剩下的部分

- (BOOL)cgHitTestForArea:(CGRect)area {
    BOOL hit = FALSE;

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    float areaFloat = ((area.size.width * 4) * area.size.height);
    unsigned char *bitmapData = malloc(areaFloat);    

    CGContextRef context = CGBitmapContextCreate(bitmapData,
                                                 area.size.width,
                                                 area.size.height,
                                                 8,
                                                 4*area.size.width,
                                                 colorspace,
                                                 kCGImageAlphaPremultipliedLast);
    CGContextTranslateCTM(context, -area.origin.x, -area.origin.y);
    [self.layer renderInContext:context];

    //Seek through all pixels.    
    float transparentPixels = 0;
    for (int i = 0; i < (int)areaFloat ; i += 4) {
        //Count each transparent pixel.
        if (((bitmapData[i + 3] * 1.0) / 255.0) == 0) {
            transparentPixels += 1;
        }
    }
    free(bitmapData);

    //Calculate the percentage of transparent pixels. 
    float hitTolerance = [[self.layer valueForKey:@"hitTolerance"]floatValue];

    NSLog(@"Apixels: %f hitPercent: %f",transparentPixels,(transparentPixels/areaFloat));

    if ((transparentPixels/(areaFloat/4)) < hitTolerance) {
        hit = TRUE;
    }    

    CGColorSpaceRelease(colorspace);
    CGContextRelease(context);

    return hit;    
}
-(BOOL)cghittestforea:(CGRect)区域{
BOOL-hit=FALSE;
CGColorSpaceRef colorspace=CGColorSpaceCreateDeviceRGB();
浮动区域浮动=((面积.大小.宽度*4)*面积.大小.高度);
无符号字符*bitmapData=malloc(areaFloat);
CGContextRef context=CGBitmapContextCreate(bitmapData,
面积、大小、宽度,
面积、大小、高度,
8.
4*面积、尺寸、宽度,
色彩空间,
KCGIMAGEAlphaPremultipledLast);
CGContextTranslateCm(上下文,-area.origin.x,-area.origin.y);
[self.layer renderInContext:context];
//搜索所有像素。
浮动透明像素=0;
对于(int i=0;i<(int)areaFloat;i+=4){
//计算每个透明像素。
如果(((位图数据[i+3]*1.0)/255.0)==0){
透明像素+=1;
}
}
免费(位图数据);
//计算透明像素的百分比。
浮点命中容差=[[self.layer valueForKey:@“命中容差”]floatValue];
NSLog(@“Apixels:%f命中率:%f”,透明像素,(透明像素/areaFloat));
if((透明像素/(面积浮点/4))

有什么建议,使这项工作请

首先,将位图图像转换为
UIColor
对象的
NSArray
,这是非常困难的。太多的开销了。改为使用像素缓冲区。学习如何使用指针


提供了一些用于执行洪水填充的简单技术的良好概述-使用递归或队列。

我还看到我尝试了许多步骤任何应用程序示例都有,请告诉我,这只是洪水填充,需要示例是wikipedia i;我以前见过这个