【发布时间】:2011-06-24 23:11:12
【问题描述】:
我有一个使用 AVCaptureSession 处理视频的应用。我喜欢写零内存泄漏,并正确处理所有对象。
这就是为什么这篇文章 - How to properly release an 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);
}
现在我来支持应用程序中断作为电话或按主页按钮。如果应用程序进入后台,我想停止捕获,并弹出我的视图控制器。
我似乎无法在 applicationDidEnterBackground 上下文中执行此操作。永远不会调用 dealloc,我的对象仍然存在,当我重新打开应用程序时,框架会开始自动进入。
我尝试使用 beginBackgroundTaskWithExpirationHandler 但无济于事。变化不大。
有什么建议吗? 谢谢!
【问题讨论】:
标签: ios4 background camera release avcapturesession