我生成从一个OpenGL层CVPixelBufferRef这样电话AVAssetWriterInputPixelBufferAdaptor的appendPixelBuffer导致EXC_BAD_ACCESS
- (CVPixelBufferRef) getGLPixelBuf {
int s = 1;
UIScreen * screen = [UIScreen mainScreen];
if ([screen respondsToSelector:@selector(scale)]){
s = (int)[screen scale];
}
const int w = self.frame.size.width/2;
const int h = self.frame.size.height/2;
const NSInteger my_data_length = 4 * w * h * s * s;
// allocate array and read pixels into it.
GLubyte * buffer = malloc(my_data_length);
GLint readType;
GLint readFormat;
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &readType);
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &readFormat);
glReadPixels(0, 0, w * s, h * s, readFormat, readType, buffer);
// gl renders "upside down" so swap top to bottom into new array.
GLubyte * buffer2 = malloc(my_data_length);
for(int y = 0; y < h*s; y++){
memcpy(buffer2 + (h * s - 1 - y) * 4 * w * s, buffer + (4 * y * w * s), 4 * w * s);
}
free(buffer);
CVPixelBufferRef pixel_buffer = NULL;
CVPixelBufferCreateWithBytes (NULL, w * 2, h * 2, kCVPixelFormatType_32BGRA, buffer2 , 4 * w * s, NULL, 0, NULL, &pixel_buffer);
free(buffer2);
return pixel_buffer;
}
,然后通过该像素缓冲区我一个AVAssetWriterInputPixelBufferAdaptor一个辅助类里面是这样的:
- (void)recordFrame {
if([recorder isRecording]){
CVPixelBufferRef pixelBuffer = [self getGLPixelBuf];
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
[recorder appendPixelBuffer:pixelBuffer withPresentationTime:camera.lastSampleTime];
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CVPixelBufferRelease(pixelBuffer);
}
}
但是在那个帮手类里面,以下
- (BOOL)appendPixelBuffer:(CVPixelBufferRef)pixelBuffer withPresentationTime:(CMTime)presentationTime {
if (writerInput.readyForMoreMediaData)
return [adaptor appendPixelBuffer:pixelBuffer withPresentationTime:presentationTime];
return NO;
}
会在执行appendPixelBuffer调用时导致EXC_BAD_ACCESS。我启用了NSZombieEnabled,但它没有提供任何信息。记录器初始化的高度和宽度与OpenGL图层的背景高度和宽度相同。该适配器配置为kCVPixelFormatType_32BGRA像素格式以及像素缓冲区。
任何帮助表示赞赏!谢谢!
返回之前pixel_buffer有你和调试检查,不要锁定像素缓冲区并发现它确切地得到错误的线? – 2011-06-16 05:16:12
您发送的参数与被调用方法中的对象相同吗? – 2011-06-16 05:17:27
我现在工作。看起来像我的像素缓冲区正在脱离上下文。我将上述两种方法合并为一种,而我不再收到EXC_BAD_ACCESS。 – davidbitton 2011-06-20 02:12:51