繁体   English   中英

无符号字符分配在偏移值目标c中为nil

[英]unsigned char allocation comes nils in offset value objective c

我正在从触摸点获取像素颜色值。 我已成功完成此操作,但有时应用程序给出了错误信息(EXC_BAD_ACCESS(CODE = 1,address = 0x41f6864)。此处的内存分配问题是供您参考的源代码。

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

您的代码中的内存管理似乎是错误的:

声明data并毫无意义地为其分配一个值:

unsigned char *data = {0};

分配一个内存块并将其引用存储在data -覆盖无意义的初始化:

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

现在获取对另一个内存块的引用,并将其存储在data ,而放弃对calloc块的引用:

data = CGBitmapContextGetData (cgctx);

做一些其他的事情,然后释放您没有calloc的块:

free(data);

如果要分配自己的内存缓冲区,则应将其传递给CGBitmapContextCreate ,但是如果您使用的是iOS 4+,则无需分配自己的缓冲区。

至于内存访问错误,您不检查point的值,并且您的计算似乎会产生不正确的offset值。 添加对pointoffset值的检查,如果它们超出范围,则应采取适当的措施(您将不得不决定应采取的措施)。

HTH

该问题可能是由导致point是出图像的rect ,所以你可以使用

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){

}

避免EXC_BAD_ACCESS

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM