Sesja AVcapture powolne uruchamianie po ponownym uruchomieniu sesji

Mam główny kontroler widoku, który dzieli się na drugi kontroler widoku, który ma avcapturesession. Pierwszy raz przechodzę od głównego kontrolera widoku do kontrolera sesji przechwytywania, zajmuje to około 50ms(sprawdzane za pomocą "instrumentów"). Następnie wracam do głównego kontrolera widoku z sesji przechwytywania, a następnie z powrotem do kontrolera avcapturesession z głównego kontrolera. Za każdym razem trwa dłużej od głównego kontrolera widoku do avcapturesession i do 5. lub 6. iteracji segue trwa około 10 sekund. (W porównaniu z 50ms po raz pierwszy.) Wkleiłem poniżej odpowiedni kod dla sesji avcapture. Czy ktoś może pomóc rozwiązać ten problem? Dzięki

Ta klasa (typu NSObject) zarządza sesją przechwytywania dla drugiego kontrolera widoku
to faktycznie implementuje avcapturession

#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

W metodzie viewDidLoad kontrolera widoku avcapture znajduje się:

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

}

Alokacja pamięci

Author: cph2117, 2013-10-15

7 answers

Napotkałem ten sam problem znalazłem ten wiersz jest głównym problemem

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

Po prostu usuń warstwę podglądu z warstwy dodatkowej podczas dealokacji i nie ma problemu z pamięcią. Moja funkcja dealokacji jest następująca

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

}

Wywołałem tę funkcję, zanim popchnąłem i popchnąłem jakikolwiek inny widok. To rozwiązało mój problem.

 10
Author: souvickcse,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-11 06:44:50

Usuwanie wejść i wyjść sesji wydaje się rozwiązać ten problem dla mnie

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

for(AVCaptureOutput *output in captureSession.outputs) {
    [captureSession removeOutput:output];
}
 5
Author: TUNER88,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-28 16:04:37

U mnie zadziałało ustawienie previewLayer typu AVCaptureVideoPreviewLayer() jako słaba zmienna, wtedy w funkcji stopCaptureSession() Mam:

func stopCaptureSession() {
    self.previewLayer?.removeFromSuperlayer()
    self.previewLayer = nil
    self.captureSession.stopRunning()
    for input in captureSession.inputs {
        self.captureSession.removeInput(input)
    }
    for output in captureSession.outputs {
        self.captureSession.removeOutput(output)
    }
}

Okazało się, że wszystkie moje problemy były związane z Podglądaczem

 3
Author: Nikita Alexander,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-11 15:28:44

Swift version TUNER88 ' s answer

func stopRecording() {
    captureSession.stopRunning()
    for input in captureSession.inputs {
        captureSession.removeInput(input)
    }
    for output in captureSession.outputs {
        captureSession.removeOutput(output)
    }
}
 2
Author: drpawelo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-13 17:50:08

Wygląda na to, że nie usuwasz warstwy podglądu w kontrolerze avcaptureViewController, który przechowywałby wewnętrzne odniesienie do sesji przechwytywania. Usuń warstwę podglądu z tej hierarchii widoków.

 0
Author: RyanR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-16 18:17:08

Swift 3

    let session = AVCaptureSession()
    if let outputMovie = outputMovie, outputMovie.isRecording {
        outputMovie.stopRecording()
    }

    self.session.stopRunning()
    if let inputs = self.session.inputs as? [AVCaptureDeviceInput] {
        for input in inputs {
            self.session.removeInput(input)
        }
    }

    if let outputs = self.session.outputs as? [AVCaptureOutput] {
        for output in outputs {
            self.session.removeOutput(output)
        }
    }
 0
Author: Giang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-24 10:01:41

Oto wersja swift TUNER88 odpowiedz

session.stopRunning()

    for(var i = 0 ; i < session.inputs.count ; i++){

        session.removeInput(session.inputs[i] as! AVCaptureInput)

    }

    for(var i = 0 ; i < session.outputs.count ; i++){

        session.removeOutput(session.outputs[i] as! AVCaptureOutput)

    }
 -1
Author: Qadir Hussain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-16 12:24:26