1

I am trying to create a simple camera app with an overlay view that shows two buttons and a gesture recognizer.

Currently I have an 'OverlayViewController' with a .xib file which contains a UIView with two buttons and a gesture recognizer.

In my CameraViewController in which I show the camera on viewDidLoad through the UIImagePicker, I try to assign this picker my overlay but so far im not getting any of the buttons in the overlay....

OverlayViewController.h

@interface OverlayViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate>

- (IBAction)take_picture:(UIButton *)sender;
- (IBAction)choose_picture:(UIButton *)sender;

@end

OverlayViewController.m -pretty much empty (do I need to initialize the UIButtons here ??)

CameraViewController.h

@interface ProfileViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@property (strong, nonatomic) UIImagePickerController *picker;
@property (strong, nonatomic) OverlayViewController *overlay;

CameraViewController.m

-(void)setupCamera
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

self.overlay = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
  // _overlay.delegate = self;


//    NSArray* nibViews= [[NSBundle mainBundle] loadNibNamed:@"OverlayViewController" owner:self.overlay options:nil];
//    UIView* uiView= [nibViews objectAtIndex:0];
//    uiView.opaque= NO;
//    uiView.backgroundColor= [UIColor clearColor];

self.picker.cameraOverlayView = self.overlay.view;
self.picker.delegate = self.overlay;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = YES;

[self presentViewController:self.picker animated:NO completion:nil];

}

Im also wondering which controller should be the delegate for these invisible buttons...

thanks for the help in advance!

Santiago
  • 1,984
  • 4
  • 19
  • 24

1 Answers1

0

There is another alternative AVCaptureSession Using it you can do your things..

put this code in viewDidAppear

//----- SHOW LIVE CAMERA PREVIEW -----
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPreset1920x1080;

CALayer *viewLayer = self.overlay.layer;
NSLog(@"viewLayer = %@", viewLayer);

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

[[captureVideoPreviewLayer connection] setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];

captureVideoPreviewLayer.frame = self.overlay.bounds;
[self.overlay.layer addSublayer:captureVideoPreviewLayer];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

[session startRunning];

and for capture image

-(UIImage *) captureNow
{
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
    for (AVCaptureInputPort *port in [connection inputPorts])
    {
        if ([[port mediaType] isEqual:AVMediaTypeVideo] )
        {
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) { break; }
}

NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection 
                                              completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
    CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
    if (exifAttachments)
    {
        // Do something with the attachments.
        NSLog(@"attachements: %@", exifAttachments);
    }
    else 
        NSLog(@"no attachments");

    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
    UIImage *image = [[UIImage alloc] initWithData:imageData];

    return image;
}];
}

here is reference link

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • thanks for the help, however I am not seeing where you used the ---CALayer *viewLayer = self.overlay.layer; ---- I am guessing that this viewLayer will be the layer of OverlayViewController which I store in CameraViewController as self.overlay however dont I have to set it to be the overlay of the captureVideoPreviewLayer?? – Santiago Jul 25 '13 at 16:42
  • I am also getting an issue with the fact that self.overlay doesnt have a property layer... What exactly do you mean to do in the line ---CALayer *viewLayer = self.overlay.layer; --- – Santiago Jul 25 '13 at 19:45
  • have you add #import ? – Toseef Khilji Jul 26 '13 at 03:58
  • if you do not added QuartzCore.framework to your project, first add this framework, and after just add #import . and 'overlay' is your view on that you had added buttons. – Toseef Khilji Jul 26 '13 at 04:07