HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 将CMSampleBufferRef转换为UIImage大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我用AVCaptureSession拍摄视频.
但我想将捕获的图像转换为UI Image.

我在互联网上找到了一些代码

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{

    NSLog(@"imageFromSampleBuffer: called");
    // Get a CMSampleBuffer's Core Video image buffer for the media data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer,0);

    // Get the number of bytes per row for the pixel buffer
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef context = CGBitmapContextCreate(baseAddress,width,height,8,bytesPerRow,colorSpace,kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);


    // Free up the context and color space
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpacE);

    // Create an image object from the Quartz image
    UIImage *image = [UIImage imageWithCGImage:quartzImage];

    // Release the Quartz image
    CGImageRelease(quartzImagE);

    return (imagE);
}

但是我遇到了一些错误

解决方法

你最好的选择是 set the capture video data output’s videoSettings一个 specifies the pixel format you want的字典,你需要设置为RGB的一些CGBitmapContext可以处理的变体.

文档有a list of all of the pixel formats that Core Video can process.CGBitmapContext只支持其中的一小部分.您在互联网上找到的代码所期望的格式是kCVPixelFormatType_32BGRA,但这可能是针对Macs-on iOS设备编写的,kCVPixelFormatType_32ARGB(big-endian)可能更快.在设备上尝试它们,并比较帧速率.

大佬总结

以上是大佬教程为你收集整理的ios – 将CMSampleBufferRef转换为UIImage全部内容,希望文章能够帮你解决ios – 将CMSampleBufferRef转换为UIImage所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。