我正面临崩溃并出现以下错误:
“CGDataProviderCreateWithCopyOfData:vm_copy失败:状态1.”
我有很多问题,你可以提供帮助。
-(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 documentation声明地址[es]必须在页面边界上。
为了确保使用正确对齐的缓冲区,您应该使用与piData
输入图像相同的步幅分配inImage
缓冲区:
size_t bytesPerRow = CGImageGetBytesPerRow(inImage);
UInt8*piData = calloc(bytesPerRow*height,sizeof(UInt8));
然后在你的bytesPerRow
循环中使用这个width*4
值而不是for
,即:
iStep = i*bytesPerRow;
这应该可以解决你的问题(注意:我假设CGImageGetWidth(inImage)
和width
是相同的)。