ios - 无法从 CVMetalTextureGetTexture 函数获取有效的 MTLTexture

标签 ios objective-c swift metal core-video

我是 Metal 新手。 我想从 CVImageBufferRef 制作 MTLTexture。 我正在使用以下代码示例来执行此操作。

guard
        let unwrappedImageTexture = imageTexture,
        let texture = CVMetalTextureGetTexture(unwrappedImageTexture),
        result == kCVReturnSuccess
    else {
        throw MetalCameraSessionError.failedToCreateTextureFromImage
    }

在这里,imageTexture:CVMetalTexture。 这是我在 Obj C 中的代码。

CVMetalTextureRef inputTexture;
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
AVAssetReaderTrackOutput track = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:video
                                                        outputSettings:@{
                                                            (NSString *)kCVPixelBufferMetalCompatibilityKey: @YES,
                                                            key:value
                                                        }];
sampleBuffer = [track copyNextSampleBuffer];
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
if(kCVReturnSuccess != CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _context.textureCache , imageBuffer, NULL, MTLPixelFormatBGRA8Unorm, width, height, 0, &inputTexture)){
    __VMLog(@"Texture Creation Error");
}

id<MTLTexture> it = CVMetalTextureGetTexture(inputTexture); //Returns nil

我的 MTLTexture 变量总是得到 nil。甚至没有发生纹理创建错误。但未生成 MTLTexture。

最佳答案

我找到了解决方案。似乎它需要一个 id 数组来获取 MTLTexture。

    //Wrong approach
    id<MTLTexture> it = CVMetalTextureGetTexture(inputTexture);

    //Right approach
    id<MTLTexture> it[1];
    it[0] = CVMetalTextureGetTexture(inputTexture);

关于ios - 无法从 CVMetalTextureGetTexture 函数获取有效的 MTLTexture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380702/

相关文章:

ios - 在方法 block 中使用方法声明中的变量时出现 "variable is not assignable (missing __block type specifier)"错误

ios - 使用 OCMockito 测试 Storyboard的最佳方式

ios - Storyboard中不同的导航栏高度

ios - 在 react-native 中使用 iOS 启动屏幕作为应用程序背景

IOS Swift 处理全局事件

swift - UTC 时间重复发生的本地通知

iphone - 在非正式协议(protocol)中委托(delegate)变量

ios - 如何检查 CLLocationCoordinate2D 不为空?

iOS 将图像和文本共享到 UIActivityViewController,然后在 Objective-C 中出现错误

ios - 添加 ADBannerView 后, Controller 不会调整 View 大小