会话重启后AVcapture会话缓慢启动

cph*_*117 14 performance ios avcapturesession

我有一个主视图控制器,它可以分割成具有avcapturesession的第二个视图控制器.我第一次从主视图控制器切换到捕获会话控制器,大约需要50ms(使用"仪器"检查).然后我从捕获会话中回到主视图控制器,然后从主控制器返回到avcapturesession控制器.每次从主视图控制器切换到avcapturesession需要更长的时间,并且通过第5次或第6次迭代,segue需要大约10秒.(与第一次50ms相比.)我已经粘贴了下面的avcapture会话的相关代码.谁能帮忙解决这个问题?谢谢

此类(类型为NSObject)管理
实际实现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
Run Code Online (Sandbox Code Playgroud)

以下是avcapture视图控制器的viewdidLoad方法:

[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];
    });

}
Run Code Online (Sandbox Code Playgroud)

内存分配

sou*_*cse 7

我遇到了同样的问题我发现这一行是主要问题

[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];
Run Code Online (Sandbox Code Playgroud)

只需在解除分配时从超级层中删除预览层,并且没有内存问题.我的解除分配功能如下

 -(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;

}
Run Code Online (Sandbox Code Playgroud)

我在弹出并推送任何其他视图之前调用了此函数.它解决了我的问题.


TUN*_*R88 5

删除会话输入和输出似乎解决了这个问题

[captureSession stopRunning];
for(AVCaptureInput *input in captureSession.inputs) {
    [captureSession removeInput:input];
}

for(AVCaptureOutput *output in captureSession.outputs) {
    [captureSession removeOutput:output];
}
Run Code Online (Sandbox Code Playgroud)