微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 如何将CGImage转换为CMSampleBufferRef?

我想将CG Image转换为CMSampleBufferRef,并使用appendSampleBuffer:方法将其附加到AVAssetWriterInput.我设法使用以下代码获取CMSampleBufferRef,但是appendSampleBuffer:在提供生成的CMSampleBufferRef时,只需返回NO.我究竟做错了什么?
- (void) appendCGImage: (CGImageRef) frame
{
    const int width = CGImageGetWidth(frame);
    const int height = CGImageGetHeight(frame);

    // Create a dummy pixel buffer to try the encoding
    // on something simple.
    CVPixelBufferRef pixelBuffer = NULL;
    CVReturn status = CVPixelBufferCreate(kcfAllocatorDefault,width,height,kCVPixelFormatType_32BGRA,NULL,&pixelBuffer);
    NSParameterassert(status == kCVReturnSuccess && pixelBuffer != NULL);

    // Sample timing info.
    CMTime frameTime = CMTimeMake(1,30);
    CMTime currentTime = CMTimeAdd(lastSampleTime,frameTime);
    CMSampleTimingInfo timing = {frameTime,currentTime,kCMTimeInvalid};

    Osstatus result = 0;

    // Sample format.
    CMVideoFormatDescriptionRef videoInfo = NULL;
    result = CMVideoFormatDescriptionCreateForImageBuffer(NULL,pixelBuffer,&videoInfo);
    NSParameterassert(result == 0 && videoInfo != NULL);

    // Create sample buffer.
    CMSampleBufferRef sampleBuffer = NULL;
    result = CMSampleBufferCreateForImageBuffer(kcfAllocatorDefault,true,videoInfo,&timing,&sampleBuffer);
    NSParameterassert(result == 0 && sampleBuffer != NULL);

    // Ship out the frame.
    NSParameterassert(CMSampleBufferDataIsReady(sampleBuffer));
    NSParameterassert([writerInput isReadyForMoreMediaData]);
    BOOL success = [writerInput appendSampleBuffer:frame];
    NSParameterassert(success); // no go :(
}

附:我知道这段代码中有内存泄漏,为了简单起见,我省略了一些代码.

解决方法

阿哈,我完全错过了 AVAssetWriterInputPixelBufferAdaptor课程,特别是将像素缓冲区配置为写入器输入.现在的代码工作,即使没有凌乱的CMSampleBuffer的东西.

原文地址:https://www.jb51.cc/iOS/336952.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐