iOS Camera overlay issues

848 Views Asked by At

I need to create custom camera overlay in my application, and I am having difficulties getting it to actually take photos, switch the camera from front to back, and cancel.

I have created a CameraOverlayViewController class, and set up all the views in storyboard.

When the user selects to take a photo, here is what I am doing to get the overlay:

CameraOverlayViewController *customerOverlay = [self.storyboard instantiateViewControllerWithIdentifier:@"camera_overlay"];

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.cameraOverlayView = customerOverlay.view;

customerOverlay.pickerRefernce = picker;

[self presentViewController:picker animated:YES completion:nil];

This works great in displaying the camera overlay as I want it to be seen.

Here is my code for CameraOverlayViewController:

#import <UIKit/UIKit.h>

@interface CameraOverlayViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) UIImagePickerController *pickerRefernce;
@property (weak, nonatomic) IBOutlet UIButton *cameraButton;
@property (weak, nonatomic) IBOutlet UIView *navBar;

- (IBAction)takePicture:(id)sender;
- (IBAction)switchCamera:(id)sender;
- (IBAction)cancelPicture:(id)sender;

@end


//iphone screen dimensions
#define SCREEN_WIDTH  self.view.bounds.size.width
#define SCREEN_HEIGTH self.view.bounds.size.height

@interface CameraOverlayViewController ()

@end

@implementation CameraOverlayViewController

@synthesize pickerRefernce = _pickerRefernce;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor clearColor];
    self.navBar.backgroundColor = [UIColor PFYellow];

    self.cameraButton.frame = CGRectMake((SCREEN_WIDTH / 2) - (self.cameraButton.frame.size.width / 2), SCREEN_HEIGTH - self.cameraButton.frame.size.height - 20, self.cameraButton.frame.size.width, self.cameraButton.frame.size.height);


}

- (IBAction)takePicture:(id)sender {

    [self.pickerRefernce takePicture];

}


- (IBAction)switchCamera:(id)sender {

    if(self.pickerRefernce.cameraDevice == UIImagePickerControllerCameraDeviceFront) {

        self.pickerRefernce.cameraDevice = UIImagePickerControllerCameraDeviceRear;

    } else {

        self.pickerRefernce.cameraDevice = UIImagePickerControllerCameraDeviceFront;

    }

}


- (IBAction)cancelPicture:(id)sender {



}

Basically when I press any button on the overlay view (including the cancel button, which has no code to cause the error), I get a major error in Xcode.

The only output is "(lldb)" and an exception is thrown in main.m which says "Thread 1: EXC_BAD_ACCESS(code = 1, address=0x69786d54)

The application just keeps running (the camera view still displays a live image) but tapping anything has no response.

Also, I have clicked the continue program execution button at least 200 times trying to get it to actually crash, but nothing happens.

All of the examples I have found online are outdated (even apples example "PhotoPicker" uses a xib file and weird outlets I am not used to) and all answers I have found here tend to explain how to get the view to show up, not how to make sure it calls all the proper methods.

Any guidance would be great, thanks!

0

There are 0 best solutions below