Memory related issue of ZBarReaderViewController in iOS 7

I use ZBarReaderViewController for scan QR Code. and it was perfectly worked on iOS 6.
But when i use iOS 7 with my project then it is not properly working with ZBarReaderViewController

Issue is related to memory, it take more then 100 MB and my device is hang at this time.

  • Weak-linking multiple frameworks for iPhone Apps (-weak_framework)
  • How to create an empty array in Swift?
  • Build an iOS app without owning a mac?
  • iOS/Objective-C - How do I query a web server database from an iPhone app?
  • Reading samples via AVAssetReader
  • console.log not working in an iOS PhoneGap 3.0 app
  • Generally in my project. user can scan QR Generator image and i have function which recognize code of QR code is related to my string which i got from server then if YES then i goes to next view controller otherwise remain in current (continue QR SCAN) screen.

    If QR code mach with my string then on next screen has “cancel” button which make be scan another code ( it means i got to previous viewController (QR SCAN)).

    At that time when i go to next viewController and back to pervious (QR Scan screen) then each time i got ZBarReaderViewController is allocated so (May be) memory related issue is generated.

    but i write code

    if(self.ZBarReaderVC)
    {
                for(UIView *subVies in self.ZBarReaderVC.cameraOverlayView.subviews)
                    [subVies removeFromSuperview];
                for(UIView *subVies in self.ZBarReaderVC.view.subviews)
                    [subVies removeFromSuperview];
                [self.ZBarReaderVC removeFromParentViewController];
                self.ZBarReaderVC = nil;
    }
    

    after [self.ZBarReaderVC dismissModalViewControllerAnimated: YES]; I remove ZBarReaderViewController at the end time then why each time i got allocated ZBarReaderViewController ???

    And also i put [self.ZBarReaderVC.readerView stop]; before dismiss ZBarReaderViewController fro stop scanning stream of reader
    but also it not worked for me.

    But i tried to solve my problem about hours of time but i am not able to solve my issue

    please help me.

    Alos i found similar problem

    Zbar SDK and ios7/xcode 5 – App is reaching 100% cpu use and memory more than 100MB

    http://sourceforge.net/p/zbar/discussion/1072195/thread/df4c215a/

    But No one can help me.

    3 Solutions Collect From Internet About “Memory related issue of ZBarReaderViewController in iOS 7”

    I found that in iOS 7 issue is occur at

    self.ZBarReaderVC.view.frame = self.view.bounds;
    

    I put break point here and check whenever i come bake from previous viewController, its take more time and also memory (issue) at this code.

    So first i need to remove view of self.ZBarReaderVC with its all subViews.. so at first i need to write

    if(self.ZBarReaderVC) // first check `self.ZBarReaderVC` is created or not?
    {
      [self.ZBarReaderVC.readerView stop]; // then stop continue scanning stream of "self.ZBarReaderVC"
      for(UIView *subViews in self.ZBarReaderVC.view.subviews) // remove all subviews
        [subViews removeFromSuperview];
      [self.ZBarReaderVC.view removeFromSuperview]; 
      self.ZBarReaderVC.view = nil;
    }
    

    And also i got that in iOS 7 self.ZBarReaderVC has remain continue scanning stream of QR Code so each time we need to stop it whenever your QR Code scanning is done and you need to dismiss your self.ZBarReaderVC then first stop scanning by [self.ZBarReaderVC.readerView stop];

    And some time user need to write/call (For do/implement some type of extra features)

    [self.ZBarReaderVC viewDidLoad];
    [self.ZBarReaderVC viewWillAppear:NO];
    [self.ZBarReaderVC viewDidAppear:NO];
    

    Methods of self.ZBarReaderVC then it is not need to use in iOS 7, so if any user who call this methods of self.ZBarReaderVC then please put it in comment.

    I hope this my suggestion is helpful for others.
    Thanks 🙂

    If you are targetting your app for iOS7 only, I ditched the ZBar component and used the native AVFoundation method, making the viewcontroller a AVCaptureMetadataOutputObjectsDelegate. Works prefectly with 3% CPU usage:

    viewcontroller.h:

    @interface viewcontroller : UIViewController <AVCaptureMetadataOutputObjectsDelegate> {
      AVCaptureSession *_session;
      AVCaptureDevice *_device;
      AVCaptureDeviceInput *_input;
      AVCaptureMetadataOutput *_output;
      AVCaptureVideoPreviewLayer *_prevLayer;
      UIView *_highlightView;
    }
    

    viewcontroller.m

    - (IBAction)btnScan:(id)sender {
      _session = [[AVCaptureSession alloc] init];
      _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
      NSError *error = nil;
    
      _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
      if (_input) {
        [_session addInput:_input];
      } else {
        NSLog(@"Error: %@", error);
      }
    
      _output = [[AVCaptureMetadataOutput alloc] init];
      [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
      [_session addOutput:_output];
    
      _output.metadataObjectTypes = [_output availableMetadataObjectTypes];
    
      _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
      _prevLayer.frame = self.view.bounds;
      _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
      [self.view.layer addSublayer:_prevLayer];
      [_session startRunning];
    }
    
    - (void)captureOutput:(AVCaptureOutput *)captureOutput 
        didOutputMetadataObjects:(NSArray *)metadataObjects 
        fromConnection:(AVCaptureConnection *)connection {
        AVMetadataMachineReadableCodeObject *barCodeObject;
        NSString *detectionString = nil;
        NSArray *barCodeTypes = @[AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode128Code,   
          AVMetadataObjectTypeQRCode];
    
        for (AVMetadataObject *metadata in metadataObjects) {
          for (NSString *type in barCodeTypes) {
            if ([metadata.type isEqualToString:type]) {
              barCodeObject = (AVMetadataMachineReadableCodeObject *)
                   [_prevLayer transformedMetadataObjectForMetadataObject:
                   (AVMetadataMachineReadableCodeObject *)metadata];
              detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
              break;
            }
          }
    
          if (detectionString != nil) {
            NSLog(@"%@", detectionString);
            [self buscarCarga:detectionString]; //Do whatever you want with the data
            [_session stopRunning];
            AVCaptureInput* input = [_session.inputs objectAtIndex:0];
            [_session removeInput:input];
            AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[_session.outputs objectAtIndex:0];
            [_session removeOutput:output];
            [_prevLayer removeFromSuperlayer];
          }
          else
            NSLog(@"No data");
        }
      }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.ZBarReaderVC = [ZBarReaderViewController new];
        self.ZBarReaderVC.readerDelegate=self;
        self.ZBarReaderVC.supportedOrientationsMask = ZBarOrientationMaskAll;
    
    
        ZBarImageScanner *scanner = self.ZBarReaderVC.scanner;
        [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];
    }
    #pragma mark - Button click method
    
    - (IBAction)startScanning:(id)sender {
    
        NSLog(@"Scanning..");
        resultTextView.text = @"Scanning..";
    
        [self presentViewController:self.ZBarReaderVC animated:YES completion:nil];
    }