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

前端之家收集整理的这篇文章主要介绍了objective-c – 如何在swift中创建VTCompressionSession?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找如何创建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,kCMVideoCodecType_H264,NULL,&session);

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

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

import Foundation
import VideoToolBox

VideoToolBox.VTCompressionSessionCreate()

println("created VTCompressionSession")

例如,此代码中断:模块“VideoToolBox”没有名为“VTCompressionSessionCreate”的成员.
只需调用VTCompressionSessionCreate就会创建错误消息Use of unresolved identifier’VTCompressionSessionCreate’.

看起来它没有暴露在swift中,因为我可以调用像VTCompressionSessionEncodeFrame这样的方法.我错过了一些明显的东西吗

解决方法

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

Compression.h

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

Compression.m

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

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

流桥接-Header.h

#import "CompressionSession.h"

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

import Cocoa
import Foundation
import VideoToolBox

let session = CreateCompressionSession()

猜你在找的cocoa相关文章