0

I am getting the pixel colour values from touch points. I am successfully doing this but after sometimes app is giving the error( EXC_BAD_ACCESS(CODE=1,address=0x41f6864). Its memory allocation problem here is the source code for your reference.

- (UIColor *) getPixelColorAtLocation:(CGPoint)point {
UIColor* color = nil;

@try{

    {

    CGImageRef inImage =  drawImage.image.CGImage;

// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL)
{
    return nil; /* error */

}  
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};

// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
   CGContextDrawImage (cgctx, rect, inImage);


// Now we can get a pointer to the image data associated with the bitmap
// context.
        unsigned char *data = {0};
       data=(unsigned char*) calloc(CGImageGetHeight(inImage) * CGImageGetWidth(inImage) , CGBitmapContextGetHeight(cgctx)*CGBitmapContextGetWidth(cgctx));

data= CGBitmapContextGetData (cgctx);






  if( data !=NULL  ) {



    //offset locates the pixel in the data from x,y.
    //4 for 4 bytes of data per pixel, w is width of one row of data.


      int offset = 4*((w*round(point.y))+round(point.x));

           // NSLog(@"%s111111",data);

     int alpha =  data[offset]; /////// EXC_BAD_ACCESS(CODE=1,address=0x41f6864)           

    int red = data[offset+1];
    int green = data[offset+2];
    int blue = data[offset+3];



    //NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
     color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
    }
// When finished, release the context
//CGImageRelease(*data);
CGContextRelease(cgctx);

// Free image data memory for the context
if (data)
{

 free(data);  
}
}
@catch (NSException *exception) {
}
return color;
}
  • It would help to show the error message as well. – Volker Feb 17 '14 at 07:01
  • Have a try: `int offset = 4*((w*round(point.y-1))+round(point.x-1));`. I am not sure if `point.y` or `point.x` starts from 0, if they don't, point.x should be in the range [1, w], point.y [1, h], or they should be [0, w-1], [0, h-1] respectively. – KudoCC Feb 17 '14 at 07:56
  • At least try to format your code properly (as an extra benefit it will be a lot easier to spot problems yourself). – Nikolai Ruhe Feb 17 '14 at 09:44

2 Answers2

1

The memory management in your code appears to be wrong:

Declare data and pointlessly assign a value to it:

unsigned char *data = {0};

Allocate a memory block and store a reference to it in data - overwriting the pointless initialisation:

data = (unsigned char *)calloc(CGImageGetHeight(inImage) * CGImageGetWidth(inImage), CGBitmapContextGetHeight(cgctx) * CGBitmapContextGetWidth(cgctx));

Now get a reference to a different memory block and store it in data, throwing away the reference to the calloc'ed block:

data = CGBitmapContextGetData (cgctx);

Do some other stuff and then free the block you did not calloc:

free(data);

If you are allocating your own memory buffer you should pass it to CGBitmapContextCreate, however provided you are using iOS 4+ there is no need to allocate your own buffer.

As to the memory access error, you are doing no checks on the value of point and your calculation would appear to be producing a value of offset which is incorrect. Add checks on the values of point and offset and take appropriate action if they are out of bounds (you will have to decide what that should be).

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
0

The problem may cause by the point is out of image rect,so you can use

try{

    int offset = 4*((w*round(point.y))+round(point.x));
    int alpha =  data[offset];       
    int red = data[offset+1];
    int green = data[offset+2];
    int blue = data[offset+3];



    color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f)
         alpha:(alpha/255.0f)];

}catch(NSException e){

}

to avoid the EXC_BAD_ACCESS

Zacks
  • 71
  • 7