【问题标题】:Bad access on CGContextClearRect对 CGContextClearRect 的访问不正确
【发布时间】:2011-12-29 20:40:44
【问题描述】:

我在下面的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

任何帮助将不胜感激。我被难住了。

【问题讨论】:

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


    【解决方案1】:

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

    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);
    

    【讨论】:

      【解决方案2】:

      检查 self.view.bounds.size 的值是否有效。可以在正确设置视图大小之前调用此方法。

      【讨论】:

      • 一开始我也是这么想的,但最终它是有效的,并且 rob 发现了我做错了什么。
      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多