我的应用程序崩溃(偶尔 - 大多数是在8到10次运行后) - 当我致电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;
}