应用程序在使用ZBar读卡器时由于内存压力而终止

时间:2014-04-03 05:11:28

标签: ios objective-c memory-management ios7 xcode5

我在我的应用中使用ZBar reader来扫描QR码。当我不断打开和关闭此扫描程序时,应用终止时会显示消息App Terminated due to Memory Pressure

我搜索了这个并得到了一个answer,其中我的应用程序面临的场景相同。即,第一次打开扫描仪有1秒延迟,第二次有2秒延迟,第三次有5秒延迟。当第四或第五个应用程序终止时。

但答案是UIImagePickerController。我正在使用ZBar阅读器。两者都有些相似。我不知道如何修改我的代码来解决它。

这是我正在使用的代码,

codeReader = [ZBarReaderViewController new];
codeReader.readerDelegate=self;
codeReader.supportedOrientationsMask = ZBarOrientationMaskAll;
codeReader.showsCameraControls = NO;
codeReader.showsZBarControls=NO;
ZBarImageScanner *scanner = codeReader.scanner;
[scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
   AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
   if ([device hasTorch] || [device hasFlash]){
     [device lockForConfiguration:nil];
     [device setTorchMode:AVCaptureTorchModeOff];
     [device setFlashMode:AVCaptureFlashModeOff];
     [device unlockForConfiguration];
   }
}
[self.view.window.rootViewController presentViewController:codeReader animated:YES completion:nil];

codeReader.cameraOverlayView = overlayview;

如何更改它以解决问题?

1 个答案:

答案 0 :(得分:1)

您可以创建ZBar阅读器的单个实例并将其作为属性存储在应用程序代理中,也可以使用UIPickerController答案中建议的单例方法。

在你的appDelegate.h

@property(强,非原子)ZBarReaderViewController * zbarReaderVC;

在appDelegate.m中的appDidFinishLaunching

self.zbarReaderVC = [ZBarReaderViewController new];
self.zbarReaderVC.readerDelegate=self;
self.zbarReaderVC.supportedOrientationsMask = ZBarOrientationMaskAll;
self.zbarReaderVC.showsCameraControls = NO;
self.zbarReaderVC.showsZBarControls=NO;
ZBarImageScanner *scanner = self.zbarReaderVC.scanner;
[scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
   AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
   if ([device hasTorch] || [device hasFlash]){
     [device lockForConfiguration:nil];
     [device setTorchMode:AVCaptureTorchModeOff];
     [device setFlashMode:AVCaptureFlashModeOff];
     [device unlockForConfiguration];
   }
}

然后,只要您需要视图

MyAppDelegate *d=[UIApplication sharedApplication].delegate;

ZBarReaderViewController codeReader=d.zbarReaderVC;

[self.view.window.rootViewController presentViewController:codeReader animated:YES completion:nil];

codeReader.cameraOverlayView = overlayview;

完成后,将其从呈现视图控制器中删除。

我还没有读过ZBar阅读器的文档,但是有一种方法可能需要调用它来启动/停止处理图像,即使它没有被呈现。我知道有ZXingObjC。