Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Camera Simulation in iOS Simulator.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 11.6k
    Comment on it

    Every now and then the Xcode developers have to make use of the iPhone camera to click pics, photos and even different types of symbols and codes like bar code and more recently the QR Codes (Quick Response codes).

    The primary problem faced by the developers in writing the code that makes use of the iPhone camera is that they need the actual device every single time to test the logical working of the code ie. to make sure that the code scans the image and then returns the correct output, the developer needs to have an actual device which might not be possible at all times.

    The regular code that makes use of the camera, when run on the iOS Simulator crashes the app giving the error :

    'NSInvalidArgumentException', reason: 'Source type 1 not available'

    This exception means that the simulator does not have a camera to execute the code and hence the app crashes.

    An easy solution to this problem is the Camera Simulation.

    As the name itself suggests, the Camera Simulation works to provide a camera to the iOS Simulator. The camera Simulator does not actually provides a camera for the Simulator but links the app to the photo gallery of the phone. Now the developer can save the images that needs to be tested, on the photo gallery of the iOS Simulator. So the developer can now import the images saved in the phone gallery and make sure that the code functions properly on the image.The developer can find out if the code scans the QR code correctly, how much time does the code takes to scan the QR code and how the result is displayed in the app. This eliminates the necessity of having an iPhone to test the code that makes use of the camera.

    The required code for the Camera Simulation is as follows:

        NSLog(@"Camera button clicked");
        resultTextView.text = @"Scanning the image";
        ZBarReaderViewController *codeReader = [[ZBarReaderViewController alloc]init];
        codeReader.readerDelegate = self;
        codeReader.supportedOrientationsMask = ZBarOrientationMaskAll;
        ZBarImageScanner *scanner = codeReader.scanner;
        [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
        [self presentViewController:codeReader animated:YES completion:nil];
    #pragma mark image picker delegates.
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
        id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
        ZBarSymbol * symbol = nil;
        for(symbol in results)
            resultTextView.text =;
            qrImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
            [picker dismissViewControllerAnimated:YES completion:nil];    

    PS.- make sure that you include the UIImagePickerControllerDelegate in the .h file of your ViewController.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: