崩溃CGDataProviderCreateWithCopyOfData:vm_copy失败:状态1

我遇到以下错误崩溃:

“CGDataProviderCreateWithCopyOfData:vm_copy失败:状态1”。

我有多个问题,你可以帮忙。

  1. 状态1在vm_copy中的含义是什么?

  2. 只有当我在数据拷贝的内部循环中设置一个断点时,才会发生这种崩溃。 然后恢复并删除断点。 如果没有断点,函数执行,但是我得到一个空白的图像。 我如何确保即使我没有设置断点,这样的崩溃被捕获,应用程序停止执行?

  3. 当我执行CGBitmapContextCreateImage时出现这个错误。 有谁知道如何解决这个问题?

-(UIImage *) convertBitmapRGBA8ToUIImage:(UInt8**)img :(int) width :(int) height { CGImageRef inImage = m_inFace.img.CGImage; UInt8*piData = calloc( width*height*4,sizeof(UInt8)); int iStep,jStep; for (int i = 0; i < height; i++) { iStep = i*width*4; for (int j = 0; j < width; j++) { jStep = j*4; piData[iStep+jStep] =img[i][j]; piData[iStep+jStep+1] =img[i][j]; piData[iStep+jStep+2] = img[i][j]; } } CGContextRef ctx = CGBitmapContextCreate(piData, CGImageGetWidth(inImage), CGImageGetHeight(inImage), CGImageGetBitsPerComponent(inImage), CGImageGetBytesPerRow(inImage), CGImageGetColorSpace(inImage), CGImageGetBitmapInfo(inImage) ); CGImageRef imageRef = CGBitmapContextCreateImage(ctx); UIImage *finalImage = [UIImage imageWithCGImage:imageRef]; CGContextRelease(ctx); CGImageRelease(imageRef); free(piData); return finalImage; } 

kern_return.h头文件给出:

 #define KERN_INVALID_ADDRESS 1 /* Specified address is not currently valid. */ 

这对应于与vm_copy failed: status 1相关的错误代码vm_copy failed: status 1

我怀疑这是一个与内存alignment有关的问题,因为vm_copy文档指出address [es]必须位于页面边界上

为了确保使用正确alignment的缓冲区,应该使用与inImageinput图像相同的步幅分配piData缓冲区:

 size_t bytesPerRow = CGImageGetBytesPerRow(inImage); UInt8*piData = calloc(bytesPerRow*height,sizeof(UInt8)); 

然后在for循环中使用这个bytesPerRow值而不是width*4 ,即:

 iStep = i*bytesPerRow; 

这应该解决您的问题(注意:我假设CGImageGetWidth(inImage)width是相同的)。