1

我正在尝试从 GPUImageVideoCamera 的 willOutputSampleBuffer:sampleBuffer 中提取帧。但是,我要么崩溃,要么生成的图像非常失真。

一些背景资料:

  • 我在运行 iOS 7.1 的 iPhone 5 上运行它

这是我用于处理每一帧的代码:

- (void)processColorImageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer {

    @autoreleasepool {
        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

        /*Lock the image buffer*/
        CVPixelBufferLockBaseAddress(imageBuffer,0);

        /*Get information about the image*/
        uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
//        size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
        size_t width = CVPixelBufferGetWidth(imageBuffer);
        size_t height = CVPixelBufferGetHeight(imageBuffer);

        /*Create a CGImageRef from the CVImageBufferRef*/
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, 4 * width, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
        CGImageRef newImage = CGBitmapContextCreateImage(newContext);

        /*We release some components*/
        CGContextRelease(newContext);
        CGColorSpaceRelease(colorSpace);

        UIImage *image = [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationUp];

        /*We relase the CGImageRef*/
        CGImageRelease(newImage);

        if (image) {
            framesTaken++;
            [self imageCaptured: image];
        }

        /*We unlock the  image buffer*/
        CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    }
}

任何帮助将不胜感激!

4

0 回答 0