gpt4 book ai didi

objective-c - 如何在 swift 中创建 VTCompressionSession?

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:46 28 4
gpt4 key购买 nike

我一直在上下寻找如何创建 VTCompressionSession( swift)在2014 WWDC Video - 'Direct Access to Video Encoding and Decoding'中提到.

以下代码适用于 Objective-C:

#import <Foundation/Foundation.h>
#import <VideoToolbox/VideoToolbox.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
VTCompressionSessionRef session;
VTCompressionSessionCreate(NULL, 500, 500, kCMVideoCodecType_H264, NULL, NULL, NULL, NULL, NULL, &session);

NSLog(@"created VTCompressionSession");
}
return 0;
}

但无论我尝试过什么,我都找不到将 VTCompressionSessionCreate 导入 swift 的方法。

import Foundation
import VideoToolbox

VideoToolbox.VTCompressionSessionCreate()

println("created VTCompressionSession")

例如,此代码中断:Module 'VideoToolbox' has no member named 'VTCompressionSessionCreate'
只需调用 VTCompressionSessionCreate 就会创建错误消息 Use of unresolved identifier 'VTCompressionSessionCreate'

看起来 API 没有在 swift 中公开,因为我可以调用像 VTCompressionSessionEncodeFrame 这样的方法就好了。我是否遗漏了一些明显的东西?

最佳答案

我的解决方法是编写一个 objective-c 函数并通过桥接 header 将其公开给 swift:

压缩.h

#import <VideoToolbox/VideoToolbox.h>
VTCompressionSessionRef CreateCompressionSession();

压缩.m

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <VideoToolbox/VideoToolbox.h>

VTCompressionSessionRef CreateCompressionSession(){
NSLog(@"CreateCompressionSession");
VTCompressionSessionRef session;
VTCompressionSessionCreate(NULL, 500, 500, kCMVideoCodecType_H264, NULL, NULL, NULL, NULL, NULL, &session);
return session;
}

stream-Bridging-Header.h

#import "CompressionSession.h"

现在您可以在 swift 中运行以下代码:

import Cocoa
import Foundation
import VideoToolbox

let session = CreateCompressionSession()

关于objective-c - 如何在 swift 中创建 VTCompressionSession?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133697/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com