-1

问题不是得到像素数据我能够找到一些来源

-(NSArray *)getRGBAtLocationOnImage:(UIImage *)theImage X:(int)x Y:(int)y
{
    // First get the image into your data buffer
    CGImageRef image = [theImage CGImage];
    NSUInteger width = CGImageGetWidth(image);
    NSUInteger height = CGImageGetHeight(image);

    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),image);
    CGContextRelease(context);

    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
    int red = rawData[byteIndex];
    int green = rawData[byteIndex + 1];
    int blue = rawData[byteIndex + 2];
    //int alpha = rawData[byteIndex + 3];

    NSLog(@"Red: %d   Green: %d    Blue: %d",red,green,blue);

    NSArray *i = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:red], [NSNumber numberWithInt:green], [NSNumber numberWithInt:blue], nil];

    free(rawData);
    return i;
}

问题是我想要获取的像素的位置。我不知道如何确定我想要获取的像素的位置。有什么方法可以解决这个问题。

4

1 回答 1

0

不确定是否理解您的问题,但是...

看看你的方法:

-(NSArray *)getRGBAtLocationOnImage:(UIImage *)theImage X:(int)x Y:(int)y {
    // Your method
}

它等待 x 和 y 并返回 i,一个包含您传递的点 (x,y) 的 RGB 数据的数组。

假设有一个 100x100 像素的图像,如果你想检查图像中的所有像素,你必须调用你的方法 10000 次(每个像素一个)。

在这种情况下,你可以尝试这样的事情:

NSMutableArray *RGBImage = [[NSMutableArray alloc] initWithObjects:nil];
    for (int k = 0; k < IMAGE_WIDTH; k++) {
        for (j = 0; j < IMAGE_HEIGHT; j++) {
            NSArray *RGBPixel = [self getRGBAtLocationOnImage:theImage X:k Y:j]
            [RGBImage addObject:RGBPixel];
        }
    }
于 2012-01-22T15:49:14.760 回答