我有一个应用程序可以正常使用以前的iOS版本,但后来我尝试在iOS7的设备上运行它,应用程序崩溃零星.我尝试使用Apple iOS7 GM SDK推荐的
Xcode 5进行构建,以升级现有的应用程序,但问题没有解决.我试图研究AVCaptureSession的Apple文档,但也没有发现任何东西
我的代码简单而标准
-(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]; } } }
应用程序在我的函数中与下一个Stacktrace崩溃:
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
解决方法
我在自定义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; }