1
我有一個使用AVCaptureSession在應用內拍照的Swift項目。我很難得到正確的語法來正確清理我的對象。在Obj-C中,建議的代碼如下:在Swift中清理AVCaptureSession
// 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)
任何幫助轉換這將是最感謝!
花了很多小時試圖弄清楚這一點後,我採取了在我的Swift項目中添加一個'CameraController.m' Objective-C類(以及相關聯的頭文件),以便連接到攝像頭源。控制器完成我需要的所有工作並存儲最後拍攝的圖像。它也會在拍攝圖像時產生通知,從而允許我的Swift類觀察通知並獲取拍攝的最後一張圖像...不是我理想的場景,但它起作用! – SimonM 2014-09-04 23:23:16