AVCaptureSession addInput: 在 iOS7 上无法工作

7

我有一个应用程序,在之前的iOS版本中工作正常,但当我尝试在iOS7设备上运行它时,应用程序会偶发性地崩溃。我尝试使用Xcode 5创建构建,这是由Apple iOS7 GM SDK推荐用于升级现有应用程序,但问题并未解决。我尝试研究苹果文档中的AVCaptureSession,但也没有找到任何信息。

我的代码简单而标准。

-(void)addVideoInput { 
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (videoDevice) {
        NSError *error;
        self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
        if ([self.captureSession canAddInput:self.videoInput]) {
            [self.captureSession addInput:self.videoInput];
        }
    }
}

我的应用在执行以下堆栈跟踪的函数时崩溃了:

0 libobjc.A.dylib 0x395adb26 objc_msgSend + 5
1 AVFoundation 0x2e33a991 <redacted> + 348
2 AVFoundation 0x2e33aca1 <redacted> + 112
3 AVFoundation 0x2e33c4d1 <redacted> + 316
4 AVFoundation 0x2e33560f <redacted> + 354
5 AVFoundation 0x2e339bf1 <redacted> + 1436
6 Foundation 0x2fdbed31 <redacted> + 272
7 Foundation 0x2fdbe9d5 <redacted> + 344
8 Foundation 0x2fdaafed <redacted> + 88
9 AVFoundation 0x2e327483 <redacted> + 94
10 AVFoundation 0x2e33c505 <redacted> + 368
11 AVFoundation 0x2e3362c7 <redacted> + 906
12 myApp 0x00156159 -[CaptureManagerUniversal addVideoInput] + 540
13 myApp 0x00154cdf -[CaptureManagerUniversal init] + 2178
14 myApp 0x00077575 -[ViewFinderViewController didAppearActions] + 312
15 myApp 0x00077b11 -[ViewFinderViewController viewDidAppear:] + 128
16 UIKit 0x3199d43b <redacted> + 410
17 UIKit 0x3199d8bd <redacted> + 264
18 UIKit 0x31a4a4cb <redacted> + 870
19 UIKit 0x31a4a15b <redacted> + 274
20 UIKit 0x319bb417 <redacted> + 178
21 UIKit 0x319bb32f <redacted> + 70
22 QuartzCore 0x31613d99 <redacted> + 232
23 libdispatch.dylib 0x39ab1d67 <redacted> + 22
24 libdispatch.dylib 0x39ab87c1 <redacted> + 268
25 CoreFoundation 0x2f47a811 <redacted> + 8
26 CoreFoundation 0x2f4790e5 <redacted> + 1300
27 CoreFoundation 0x2f3e3cd7 CFRunLoopRunSpecific + 522
28 CoreFoundation 0x2f3e3abb CFRunLoopRunInMode + 106
29 GraphicsServices 0x33e602db GSEventRunModal + 138
30 UIKit 0x319e8121 UIApplicationMain + 1136
31 myApp 0x00097a9d main + 11 6

可能你正在释放一个已经被释放的对象(隐式地在自动释放池中..等)。 - iPatel
1
@iPatel,你是对的!我使用了Instruments来检测僵尸对象,结果是:一个Objective-C消息被发送到了一个已释放的'CaptureManagerUniversal'对象(zombie),地址为:0x1568df30。谢谢。我认为iOS7与之前的版本有很多不同之处。 - andproff
1个回答

10

我在自定义的CaptureManager中添加了dealloc方法:

[self.captureSession removeInput:self.videoInput];
[self.captureSession removeOutput:self.videoOutput];

之前

self.captureSession = nil;
self.videoOutput = nil;
self.videoInput = nil;

对我来说它有效(iOS7也是如此)

我现在的dealloc方法是:

- (void)dealloc {
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter removeObserver:[self deviceConnectedObserver]];
    [notificationCenter removeObserver:[self deviceDisconnectedObserver]];
    [notificationCenter removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

    [self.captureSession stopRunning];
    [self.captureSession removeInput:self.videoInput];
    [self.captureSession removeOutput:self.videoOutput];
    self.captureSession = nil;
    self.videoOutput = nil;
    self.videoInput = nil;
}

+1!非常感谢,你也帮我解决了问题。我在使用AVCaptureVideoDataOutput时遇到了麻烦。当我释放相机类时,在IOS7上我的应用程序崩溃了。在得到你的答案后,我还删除了添加到会话中的输入和输出,现在我的应用程序在所有IOS版本上都可以正常工作了。 - Surjeet Singh
我很高兴这个解决方案对你有帮助 :) - andproff
我的问题也解决了,我完全不知道怎么办 :) - mahmud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接