Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c CGContextDrawImage因奇数宽度值而崩溃_Objective C_Image_Macos_Core Graphics_Cgimage - Fatal编程技术网

Objective c CGContextDrawImage因奇数宽度值而崩溃

Objective c CGContextDrawImage因奇数宽度值而崩溃,objective-c,image,macos,core-graphics,cgimage,Objective C,Image,Macos,Core Graphics,Cgimage,我尝试在Mac OSX Yosemite上使用CGContextDrawImage绘制CGImageRef。 当我的图像宽度为偶数时,一切正常,但当图像宽度为偶数时,即使一切正常(图像和位图上下文在内存中),程序也会因EXC_BAD_访问而崩溃 这是我的密码: unsigned long rowBytes = ((CGImageGetBitsPerPixel(image) * CGImageGetWidth(image)) + 15) & ~15; /* TRIED THIS WITH

我尝试在Mac OSX Yosemite上使用CGContextDrawImage绘制CGImageRef。 当我的图像宽度为偶数时,一切正常,但当图像宽度为偶数时,即使一切正常(图像和位图上下文在内存中),程序也会因EXC_BAD_访问而崩溃

这是我的密码:

unsigned long rowBytes = ((CGImageGetBitsPerPixel(image) * CGImageGetWidth(image)) + 15) & ~15;

/* TRIED THIS WITH SAME RESULTS
unsigned long rowBytes = CGImageGetWidth(image) * 4;

if (rowBytes % 16) {

    rowBytes = ((rowBytes / 16) + 1) * 16;

}
*/

void *baseAddress = valloc(CGImageGetHeight(image) * rowBytes);

if (baseAddress == NULL) {

    CGImageRelease(image);

    return nil;

}

CGContextRef bitmapContext = CGBitmapContextCreate(baseAddress,
                                                   CGImageGetWidth(image),
                                                   CGImageGetHeight(image),
                                                   8,
                                                   rowBytes,
                                                   [context colorSpace],
                                                   kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);


if (bitmapContext == NULL) {

    free(baseAddress);
    CGImageRelease(image);

    return nil;

}


CGRect bounds = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));
CGContextClearRect(bitmapContext, bounds);
CGContextDrawImage(bitmapContext, bounds, image);
当崩溃出现时,libRIP库似乎与以下消息有关:

线程0崩溃::调度队列:com.apple.main-Thread 0
com.apple.CoreGraphics 0x00007fff8124aaa0解码字节8bpc 3+ 375 1 com.apple.CoreGraphics 0x00007fff8123a2af解码数据 +19394 2 com.apple.CoreGraphics 0x00007fff81234c84 img_decode_read+380 3 com.apple.CoreGraphics
0x00007fff812349ff图像颜色匹配读取+379 4
com.apple.CoreGraphics 0x00007fff8121a40d img_数据_锁+8512 5 com.apple.CoreGraphics 0x00007fff8121726e CGSImageDataLock +151 6 libRIP.A.dylib 0x00007fff8c8d52d2 ripc_acquiredimage+906 7 libRIP.A.dylib
0x00007fff8c8d3df5 ripc_DrawImage+1037 8 com.apple.CoreGraphics
0x00007fff81216e27 CGContextDrawImage+457

我认为这是一个行字节值问题,但每次尝试使用此值进行管理都失败。
是否有人遇到过同样的问题并解决了它?我遗漏了什么?

如果我理解正确,您有一个名为
image
的图像,您要做的第一件事是计算一行中的字节数。你不应该自己做这件事!只有图像本身知道它对一行使用了多少字节
CGimage
和朋友们有自己的策略来定义这个数字,它可能(而且确实)从一个操作系统版本更改到下一个版本。直接询问图像

unsigned long rowBytes = CGImageGetBytesPerRow(image);
试试这个,看看你的代码和我的提示的结果是否不同


我不是舒尔,如果这会杀死破碎机。试试看。

我认为每行的字节数必须是16的倍数。。。嗯,直接从图像中获取每行的字节数也不起作用。您当前的计算
((CGImageGetBitsPerPixel(image)*CGImageGetWidth(image))+15)和~15
计算每行的比特数,而不是字节数。在任何情况下,除非您试图强制每行一个特定字节,否则应该为
数据
参数传递
NULL
,为
CGBitmapContextCreate()
bytesPerRow
参数传递0,以便它管理缓冲区并计算每行的最佳字节。最后,从堆栈跟踪来看,我怀疑问题在于源图像,而不是位图上下文。CG可能会推迟使用该数据,直到它被绘制出来,因此届时会出现问题。