iphone - 用颜色填充图像的一部分

标签 iphone ios

我正在做一个 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;
}

我正在使用公差值获取数据。在这里,我正在努力绘制剩余部分。

- (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;    
}

有什么建议可以使这项工作成功吗?

最佳答案

首先,将位图图像转换为 UIColor 对象的 NSArray 很疯狂。方式,太多的开销。改为使用像素缓冲区。了解如何使用指针。

http://en.wikipedia.org/wiki/Flood_fill#The_algorithm很好地概述了一些使用递归或队列执行泛洪填充的简单技术。

关于iphone - 用颜色填充图像的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8124308/

相关文章:

iphone - 使用加速度计测量小距离 - iOS

iphone - magento REST API 在 iPhone 中无法访问

iphone - 部分用户的UILocalNotification音量较低,有什么方法可以控制吗?

ios - 如何实现执行 contentStretch 逆操作的 UIView?

objective-c - 属性 - Objective-C 中变量的实现

iphone - 带蓝牙或免提功能的 CTCall

objective-c - 继续类中的 get Property 属性与主类不匹配

iphone - UIPicker 的替代品

iphone - 将点从图像转换为 UIImageView 点,contentMode-aware

iphone - 解析存储在 iPhone 应用程序的文档目录中的 XML 文件