0

当我调用CFDataGetBytePtr. 基本上,当我可以进一步操作时,我想从图像中复制像素字节并存储在缓冲区中。是因为我不释放m_PixelBuf吗?还是复制数据的方式不对?

+ (int**) UIImage2GrayInt:(UIImage*)inImage
{
CGImageRef cgImage = inImage.CGImage;
CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));  
UInt8 *m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);  
int m_iH = CGImageGetHeight(cgImage);
int m_iW = CGImageGetWidth(cgImage);
//short* m_inDataGrey = malloc(sizeof(short)*(m_iW*m_iH));

int**piData = [self initVal:m_iH:m_iW];
//Convert GreyScale and copy data
int iStep,jStep;


for (int i = 0; i < m_iH; i++) 
{
    iStep = i*m_iW*4;
    //iStepGrey = i*m_iW;
    for (int j = 0; j < m_iW; j++) 
    {
        jStep = j*4;
        piData[i][j] =   ((double)m_PixelBuf[iStep + jStep] + (double)m_PixelBuf[iStep + jStep +1] + (double)m_PixelBuf[iStep + jStep +2])/3.0;
    }
}

m_PixelBuf = NULL;
CFRelease(m_DataRef);
return piData;
}

这是崩溃日志

1.  0 MyApp 0x000606ae testflight_backtrace + 238
    2.  1 MyApp 0x00061398 TFSignalHandler + 264
    3.  2 libsystem_c.dylib 0x328b87ec _sigtramp + 48
    4.  3 MyApp 0x00046fa4 +[clsGlobalHelper UIImage2GrayInt:] (clsGlobalHelper.m:276)
    5.  4 MyApp 0x00046fa4 +[clsGlobalHelper UIImage2GrayInt:] (clsGlobalHelper.m:276)
    6.  5 MyApp 0x00046d40 +[clsGlobalHelper resizeImageAvg:::] (clsGlobalHelper.m:212)
    7.  6 MyApp 0x00036fd2 -[clsNewPhoto ProcessSelectedFace] (clsFirstViewController.m:618)
    8.  7 MyApp 0x000354ae -[clsNewPhoto btnSelectGender:] (clsFirstViewController.m:309)
    9.  8 CoreFoundation 0x354063fc -[NSObject performSelector:withObject:withObject:] + 52

这是代码initval

+(int**) initVal:(int) Nr:( int) Nc 
{
  int ** p;
  p = calloc(Nr,sizeof(int*));

  for(int i = 0; i < Nr; i++)
  {
    p[i] = calloc(Nc,sizeof(int));

  }
  return p;
}
4

1 回答 1

0

这个问题的答案帮助我解决了这个问题。仍然不知道 CFDataGetBytePtr 有什么问题,但至少我有前进的解决方案。

于 2012-10-26T03:10:02.803 回答