我有一个主视图控制器,它连接到具有 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];
});
}
我遇到了同样的问题我发现这条线是主要问题
[[[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;
}
我在弹出和推送任何其他视图之前调用了这个函数。它解决了我的问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)