Pointers AVCaptureSession在Swift中清理
我有一个Swift项目,它使用AVCaptureSession在应用程序中拍照。我很难获得正确的语法来正确清理对象。在Obj-C中,建议的代码如下:Pointers AVCaptureSession在Swift中清理,pointers,swift,avcapturesession,Pointers,Swift,Avcapturesession,我有一个Swift项目,它使用AVCaptureSession在应用程序中拍照。我很难获得正确的语法来正确清理对象。在Obj-C中,建议的代码如下: // Releases the object - used for late session cleanup static void capture_cleanup(void* p) { NewPostPreviewViewController* csc = (NewPostPreviewViewController*)p; [c
// Releases the object - used for late session cleanup
static void capture_cleanup(void* p)
{
NewPostPreviewViewController* csc = (NewPostPreviewViewController*)p;
[csc release]; // releases capture session if dealloc is called
}
// Stops the capture - this stops the capture, and upon stopping completion releases self.
- (void)stopCapture {
// Retain self, it will be released in capture_cleanup. This is to ensure cleanup is done properly,
// without the object being released in the middle of it.
[self retain];
// Stop the session
[session stopRunning];
// Add cleanup code when dispatch queue end
dispatch_queue_t queue = dispatch_queue_create("VideoDataOutputQueue", NULL);
dispatch_set_context(queue, self);
dispatch_set_finalizer_f(queue, capture_cleanup);
[dataOutput setSampleBufferDelegate: self queue: queue];
dispatch_release(queue);
}
终结器将调用capture\u cleanup
,但我不知道如何设置上下文或指向capture\u cleanup
函数的指针(甚至不知道函数定义是什么样子)
到目前为止,我已经尝试了以下方法,但我甚至不相信自己走上了正确的道路
let p: UnsafeMutablePointer<NewPostPreviewViewController> = UnsafeMutablePointer.alloc(sizeof(NewPostPreviewViewController))
p.initialize(self)
var videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", nil)
dispatch_set_context(videoDataOutputQueue, p)
dispatch_set_finalizer_f(videoDataOutputQueue, ????);
self.videoDataOutput!.setSampleBufferDelegate(self, queue: videoDataOutputQueue)
dispatch_release(videoDataOutputQueue)
让p:UnsafeMutablePointer=UnsafeMutablePointer.alloc(sizeof(newPostReviewViewController))
p、 初始化(自我)
var videoDataOutputQueue=dispatch\u queue\u create(“videoDataOutputQueue”,nil)
调度设置上下文(videoDataOutputQueue,p)
调度集终结器(videoDataOutputQueue);
self.videoDataOutput!。setSampleBufferDelegate(自身,队列:videoDataOutputQueue)
调度发布(videoDataOutputQueue)
任何帮助都将不胜感激 使用桥接到Objective-C代码解决。通过在我的Swift项目中包含一个CameraController.m Objective-C类(带有相关的标题),我建立了访问摄影机提要的桥接
Objective-C类完成了我需要的所有工作,并存储了最后拍摄的图像。拍摄完图像后,它还会生成一个通知,允许我的Swift类观察该通知并获取最后一张拍摄的图像。花了很多时间试图弄清楚这一点后,我求助于在我的Swift项目中包括一个
CameraController.m
Objective-C类(带有相关标题)我连接到它以访问摄像头提要。控制器完成我需要的所有工作,并存储最后拍摄的图像。一旦拍摄到图像,它还会生成一个通知,允许我的Swift类观察通知并获取最后拍摄的图像。。。这不是我的理想方案,但它是有效的!