iphone - CGContextClearRect 访问错误

标签 iphone objective-c cocoa-touch ios5 core-graphics

我在下面的 CGContextClearRect(c, self.view.bounds) 行上收到错误 EXC_BAD_ACCESS。我似乎不明白为什么。这是在 UIViewController 类中。这是我在崩溃期间所处的功能。

- (void)level0Func {
printf("level0Func\n");
frameStart = [NSDate date];

UIImage *img;
CGContextRef c = startContext(self.view.bounds.size);
printf("address of context: %x\n", c);
/* drawing/updating code */ {
    CGContextClearRect(c, self.view.bounds); // crash occurs here
    CGContextSetFillColorWithColor(c, [UIColor greenColor].CGColor);
    CGContextFillRect(c, self.view.bounds);

    CGImageRef cgImg = CGBitmapContextCreateImage(c);
    img = [UIImage imageWithCGImage:cgImg]; // this sets the image to be passed to the view for drawing
    // CGImageRelease(cgImg);
}
endContext(c);


}

这是我的 startContext() 和 endContext():

CGContextRef createContext(int width, int height) {
CGContextRef r = NULL;
CGColorSpaceRef colorSpace;
void *bitmapData;
int byteCount;
int bytesPerRow;

bytesPerRow = width * 4;
byteCount = width * height;

colorSpace = CGColorSpaceCreateDeviceRGB();
printf("allocating %i bytes for bitmap data\n", byteCount);
bitmapData = malloc(byteCount);

if (bitmapData == NULL) {
    fprintf(stderr, "could not allocate memory when creating context");
    //free(bitmapData);
    CGColorSpaceRelease(colorSpace);
    return NULL;
}

r = CGBitmapContextCreate(bitmapData, width, height, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);

return r;
}

CGContextRef startContext(CGSize size) {
    CGContextRef r = createContext(size.width, size.height);
    // UIGraphicsPushContext(r); wait a second, we dont need to push anything b/c we can draw to an offscreen context
    return r;
}

void endContext(CGContextRef c) {
    free(CGBitmapContextGetData(c));
    CGContextRelease(c);
}

我基本上想做的是绘制一个我没有插入堆栈的上下文,这样我就可以从中创建一个 UIImage。这是我的输出:

wait_fences: failed to receive reply: 10004003
level0Func
allocating 153600 bytes for bitmap data
address of context: 68a7ce0

如有任何帮助,我们将不胜感激。我被难住了。

最佳答案

您没有分配足够的内存。以下是代码中的相关行:

bytesPerRow = width * 4;
byteCount = width * height;
bitmapData = malloc(byteCount);

当您计算bytesPerRow时,您(正确地)将宽度乘以4,因为每个像素需要4个字节。但是,当您计算 byteCount 时,您不会乘以 4,因此您的行为就好像每个像素只需要 1 个字节。

将其更改为:

bytesPerRow = width * 4;
byteCount = bytesPerRow * height;
bitmapData = malloc(byteCount);

或者,不要分配任何内存,Quartz 将为您分配正确的内存量,并为您释放它。只需将 NULL 作为 CGBitmapContextCreate 的第一个参数传递即可:

r = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);

关于iphone - CGContextClearRect 访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8673172/

相关文章:

objective-c - 在串口上用read()操作取消线程

ios - 关闭自动布局似乎会改变屏幕的高度

iphone - 移动容器 UIView 时 UITableView 调整大小

iphone - 我可以在等待审核时更新我的​​二进制文件,而不会被扔回等待队列吗?

iphone - ios 中的放大和缩小功能

objective-c - 为什么 protocol_* 方法不适用于 Clang + Linux 上的现代 GCC-Runtime?

ios - FBLoginView 委托(delegate)方法不再被调用

iphone - iOS 应用程序事件处理程序的正确位置(MVC 方面)是什么?

iphone - Xcode 4.02 在构建大型项目期间始终挂起

objective-c - 目前是否有适用于 iOS4 和 Xcode4 的 BDD 解决方案?