0

I'm using ZBar reader in my app for scanning QR Code. When I continuously open and close this scanner, the app terminated with message App Terminated due to Memory Pressure.

I searched over this and got an answer in which the same scenario that my app faces. ie, first time opening the scanner had a 1 second delay, second had a 2 second delay, third had a 5 second delay. and when going for fourth or fifth the app terminated.

But that answer is for UIImagePickerController. I'm using ZBar reader. Both are somewhat similar. I don't know how to modify my code to solve it.

Here is the code that I'm using,

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;

How should I change it to solve the issue?

Community
  • 1
  • 1
Nazik
  • 8,696
  • 27
  • 77
  • 123
  • You can use the 'leaks' instrument to try and identify where you are leaking memory. The other thing you should do is see if you can create a single instance of a ZBar reader and the activiate/deactivate it rather than continually creating a new one as this is expensive in terms of time and memory. In one of my apps I am using ZXingObjC and this is the approach I take to make things faster – Paulw11 Apr 03 '14 at 05:19
  • Probably the easiest is to store in on a property of your app delegate and allocate it in your didFinishLaunching method. – Paulw11 Apr 03 '14 at 05:30

1 Answers1

1

You can create a single instance of your ZBar reader and store it as a property on your app delegate or you can use the singleton approach that was suggested in the UIPickerController answer.

In your appDelegate.h

@property (strong,nonatomic) ZBarReaderViewController *zbarReaderVC;

In your appDidFinishLaunching in appDelegate.m

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];
   }
}

Then whenever you need the view

MyAppDelegate *d=[UIApplication sharedApplication].delegate;

ZBarReaderViewController codeReader=d.zbarReaderVC;

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

codeReader.cameraOverlayView = overlayview;

When you have finished with it remove it from the presenting view controller.

I haven't read through the docs of ZBar reader, but there is probably a method you need to call to start/stop it processing images even when it isn't presented. I know there is with ZXingObjC.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Thanks for the idea. I used the above code in viewdidload function that the view controller will be loaded only one time and the `imagePickerController didFinishPickingMediaWithInfo` should be there to get the image. Anyway the memory issue got fixed. – Nazik Apr 03 '14 at 06:21