我有一个主视图控制器,它连接到具有 avcapturesession 的第二个视图控制器。我第一次从主视图控制器到捕获会话控制器时,大约需要 50 毫秒(使用“仪器”检查)。然后我从捕获会话回到主视图控制器,然后从主控制器回到 avcapturesession 控制器。每次从主视图控制器到 avcapturesession 需要更长的时间,到第 5 次或第 6 次迭代时,segue 大约需要 10 秒。(与第一次为 50 毫秒相比。)我在下面粘贴了 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
以下是在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];
});
}