AVCaptureSessionを使用してビデオを処理するアプリがあります。私は、メモリリークがなく、すべてのオブジェクトを適切に処理して書き込むのが好きです。
そのため、この投稿-AVCaptureSessionを適切に解放する方法-は非常に役立ちました- [session stopRunning]は非同期であるため、セッションを停止して保持オブジェクトを解放し続けることはできません。
これで解決しました。これはコードです:
// Releases the object - used for late session cleanup
static void capture_cleanup(void* p)
{
CaptureScreenController* csc = (CaptureScreenController*)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("capture_screen", NULL);
dispatch_set_context(queue, self);
dispatch_set_finalizer_f(queue, capture_cleanup);
[dataOutput setSampleBufferDelegate: self queue: queue];
dispatch_release(queue);
}
今では、電話やホームボタンの押下などでアプリの中断をサポートするようになりました。アプリケーションがバックグラウンドに入った場合は、キャプチャを停止して、ViewControllerをポップします。
applicationDidEnterBackgroundコンテキストでは実行できないようです。Deallocが呼び出されることはなく、オブジェクトは存続します。アプリを再度開くと、フレームが自動的に着信し始めます。
beginBackgroundTaskWithExpirationHandlerを使用してみましたが、役に立ちませんでした。あまり変わりませんでした。
助言がありますか?ありがとう!