gpt4 book ai didi

ios - 如何在 iOS 中初始化该协议(protocol)?

转载 作者:行者123 更新时间:2023-11-30 13:54:05 27 4
gpt4 key购买 nike

这是protocol .

这就是我目前使用它的方式。但是,当我尝试访问它时,它崩溃了,因为我没有正确初始化它。

import Foundation
import OpenTok
import GPUImage

class FilteredPublisher: OTPublisherKit, OTVideoCapture, GPUImageVideoCameraDelegate {
weak var videoCaptureConsumer: OTVideoCaptureConsumer! // Is this right?

let imageHeight = 480
let imageWidth = 640
var videoCamera: GPUImageVideoCamera?
var videoFrame = OTVideoFrame()
var view = GPUImageView()

override init() {
super.init()
}

override init!(delegate: OTPublisherKitDelegate!) {
super.init(delegate: delegate)
}

override init!(delegate: OTPublisherKitDelegate!, name: String!, audioTrack: Bool, videoTrack: Bool) {
super.init(delegate: delegate, name: name, audioTrack: audioTrack, videoTrack: videoTrack)
}

override init!(delegate: OTPublisherKitDelegate!, name: String!){
super.init(delegate: delegate, name: name)

self.view = GPUImageView(frame: CGRectMake(0,0,1,1))
self.videoCapture = self

let format = OTVideoFormat()
format.pixelFormat = OTPixelFormat.NV12
format.imageWidth = UInt32(imageWidth)
format.imageHeight = UInt32(imageHeight)
self.videoFrame = OTVideoFrame(format: format)
}


func willOutputSampleBuffer(sampleBuffer: CMSampleBuffer!) {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, 0)
videoFrame?.clearPlanes()


for var i = 0 ; i < CVPixelBufferGetPlaneCount(imageBuffer!); ++i {
videoFrame?.planes.addPointer(CVPixelBufferGetBaseAddressOfPlane(imageBuffer!, i))
}
videoFrame?.orientation = OTVideoOrientation.Left

videoCaptureConsumer.consumeFrame(videoFrame) //this crashes the app
CVPixelBufferUnlockBaseAddress(imageBuffer!, 0)
}
}

当我运行我的应用程序时,一切都会构建。但是,它在这一行崩溃:videoCaptureConsumer.consumeFrame(videoFrame)

是因为我没有正确初始化videoCaptureConsumer吗? OTVideoCaptureConsumer 没有 init(),所以我不知道该怎么做。

This是我的文件。 This是我翻译过来的 Objective-C 版本。

最佳答案

这是一个隐式解包的可选:

weak var videoCaptureConsumer: OTVideoCaptureConsumer!

作为程序员,您在这里基本上要说的是,在代码中您选择调用此对象的方法的任何位置,该对象都不会为零。如果你在任何地方调用这个对象的方法并且该对象恰好为零,那么你的应用程序将会崩溃。隐含地,如果您尝试调用 videoCaptureConsumer 上的方法并且该方法为零,那么您是在说这是一个错误。

如果您在调用 consumeFrame: 时发生崩溃,这些是故障点(按发生顺序排列):

  1. videoCaptureConsumer 属性未在任何地方设置。这需要初始化一个实现 OTVideoCaptureConsumer 协议(protocol)的对象。
  2. 正在设置的对象在调用 consumeFrame: 之前被释放。如果没有其他人持有对该对象的引用,就会发生这种情况。
  3. 该对象仍然存在,但它没有实现方法 consumeFrame:,或者方法签名错误(例如,它在应该接受输入时不接受输入)。
  4. 该对象的 consumeFrame: 实现包含一个错误。

关于ios - 如何在 iOS 中初始化该协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883872/

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