问题描述
我有一个主视图控制器,它会转向第二个具有avcapturesession的视图控制器。我第一次从主视图控制器切换到捕获会话控制器,大约需要50ms(使用仪器检查)。然后我从捕获会话中回到主视图控制器,然后从主控制器返回到avcapturesession控制器。每次从主视图控制器切换到avcapturesession需要更长的时间,并且通过第5或第6次迭代,segue大约需要10秒。 (与第一次50ms相比。)我已经粘贴了下面的avcapture会话的相关代码。谁能帮忙解决这个问题?谢谢
I have a main view controller and that segues to a second view controller that has an avcapturesession. The first time I segue from the main view controller to the capture session controller, it takes about 50ms (checked using 'instruments'). Then I segue back to the main view controller from the capture session and then back to the avcapturesession controller from the main controller. Each time it takes longer to segue from the main view controller to the avcapturesession and by the 5th or 6th iteration the segue takes about 10 seconds. (Compared with 50ms for first time.) I've pasted the relevant code for the avcapture session below. Can anyone help solve this? Thanks
此类(类型为NSObject)管理第二个视图控制器的捕获会话
实际实现了avcapturesession
This class (of type NSObject) manages the capture session for the second view controller
that actually implements the avcapturesession
#import "CaptureSessionManager.h"
@implementation CaptureSessionManager
@synthesize captureSession;
@synthesize previewLayer;
#pragma mark Capture Session Configuration
- (id)init {
if ((self = [super init])) {
[self setCaptureSession:[[AVCaptureSession alloc] init]];
}
return self;
}
- (void)addVideoPreviewLayer {
[self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]];
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
}
- (void)addVideoInput {
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice) {
NSError *error;
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (!error) {
if ([[self captureSession] canAddInput:videoIn])
[[self captureSession] addInput:videoIn];
//else
// NSLog(@"Couldn't add video input");
}
// else
// NSLog(@"Couldn't create video input");
}
//else
// NSLog(@"Couldn't create video capture device");
}
- (void)dealloc {
[[self captureSession] stopRunning];
[previewLayer release], previewLayer = nil;
[captureSession release], captureSession = nil;
[super dealloc];
}
@end
以下是在viewdidLoad中avcapture视图控制器的方法:
The following is in the viewdidLoad method of the avcapture view controller:
[self setCaptureManager:[[CaptureSessionManager alloc] init]];
[[self captureManager] addVideoInput];
[[self captureManager] addVideoPreviewLayer];
CGRect layerRect = [[[self view] layer] bounds];
[[[self captureManager] previewLayer] setBounds:layerRect];
[[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];
[[captureManager captureSession] startRunning];
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:YES];
[[[self captureManager] previewLayer]removeFromSuperlayer];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[captureManager captureSession] stopRunning];
});
}
推荐答案
我遇到了同样的问题我找到了这一行是主要问题
I have encounter the same problem I have found this line is the main problem
[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];
在解除分配时,只需从超级层删除预览层,并且没有内存问题。我的解除分配函数如下:
Just remove the previewlayer from the super layer while deallocating and there is no memory issue. My deallocating function is as follow
-(void)deallocSession
{
[captureVideoPreviewLayer removeFromSuperlayer];
for(AVCaptureInput *input1 in session.inputs) {
[session removeInput:input1];
}
for(AVCaptureOutput *output1 in session.outputs) {
[session removeOutput:output1];
}
[session stopRunning];
session=nil;
outputSettings=nil;
device=nil;
input=nil;
captureVideoPreviewLayer=nil;
stillImageOutput=nil;
self.vImagePreview=nil;
}
我在弹出并推送任何其他视图之前调用了此函数。它解决了我的问题。
i called this function before popping and pushing any other view. It solved my issue.
这篇关于会话重启后AVcapture会话缓慢启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!