ios - session 重启后 AVcapture session 启动缓慢

标签 ios avcapturesession performance

我有一个主视图 Controller ,它连接到具有 avcapturesession 的第二个 View Controller 。我第一次从主视图 Controller 转到捕获 session Controller 时,大约需要 50 毫秒(使用“仪器”检查)。然后我从捕获 session 返回到主视图 Controller ,然后从主 Controller 返回到 avcapturesession Controller 。每次从主视图 Controller segue 到 avcapturesession 需要更长的时间,到第 5 次或第 6 次迭代时,segue 大约需要 10 秒。 (与第一次 50ms 相比。)我已经在下面粘贴了 avcapture session 的相关代码。谁能帮忙解决这个问题?谢谢

此类(NSObject 类型)管理第二个 View Controller 的捕获 session
实际实现了 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

下面是在avcapture View Controller 的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];
    });

}

Memory allocation

最佳答案

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

[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];

只需在释放时从超层中删除预览层,就不会出现内存问题。我的解除分配函数如下

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

}

我在弹出和推送任何其他 View 之前调用了这个函数。它解决了我的问题。

关于ios - session 重启后 AVcapture session 启动缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391300/

相关文章:

iOS Firebase Swift,在 OnDisconnect 上使用以监控在线状态不起作用

ios - 使用 FirebaseStorageUI + SDWebImage 时出错

swift - AVCaptureMetadataObjectDelegate 未收到回调

python - 为什么这个 tensorflow 训练需要这么长时间?

java - lombok会降低性能吗?

ios - RXSwift 未订阅主线程

iphone - 有没有办法通过 Objective-C 中的委托(delegate)发送对象

java - 为什么消费者会降低生产者的绩效

ios - AVFoundation 在 iPhone 12 上录制 10 位 HDR 视频

objective-c - 如何在主线程被阻塞时更新 CALayer 的内容?