我有一个使用AVCaptureSession在应用程序内拍照的Swift项目。我很难获得正确的语法来正确清理我的对象。在Obj-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_cleanup
,但我不知道如何设置上下文或指向capture_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)
任何转换此内容的帮助都会非常感激!
答案 0 :(得分:0)
解决使用桥接到Objective-C代码。通过在我的Swift项目中包含一个CameraController.m Objective-C类(带有相关标题),我可以桥接访问摄像机源。
Objective-C类完成我需要的所有工作,并存储最后拍摄的图像。它还会在拍摄图像后生成通知,允许我的Swift类观察通知并获取最后拍摄的图像。