Question

Im trying to get RGB values of an image at particular point. Sometimes it get crashed by bad access.

+ (void)pixelValueForData:(unsigned char*)data
               atOffset:(int)offset {
        int red = data[offset+1];
        int green = data[offset+2];
        int blue = data[offset+3];
        NSLog(@"offset: %i colors: RGB A %i %i %i ",offset,red,green,blue);
}

offset calculated as

                int offset = 4*((width*y)+x);

What should i do to avoid that crash ?

Was it helpful?

Solution

Yes it was problem with calculating offset. It should obey conditions x >= 0, x <= width , y >=0 and y <= height. offset satisfying above conditions only should pass to pixelValueForData: method. Else it get crashed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top