8

I was wondering if anyone was willing to share how to put a Camera feature into an iOS app, or if anyone knew of a simple tutorial. NOT one with any buttons, just showing on the screen what the camera is seeing. I tried Apple's Documentation, but it was too complex for my needs.

Thank you so much!

EDIT: Any simple tutorial will do fine. Like I said, I don't need anything else, besides it to display what the camera is seeing.

Branch
  • 387
  • 5
  • 16
  • http://stackoverflow.com/questions/14185126/take-a-picture-in-ios-without-uiimagepicker-and-without-preview-it – brandonscript Dec 19 '13 at 23:32
  • That is not what I need. I would like my app to have NO buttons whatsoever. @r3mus – Branch Dec 19 '13 at 23:34
  • Read it. "You can hide the controls of a `UIImagePickerController` by using `[myImagePickerController setShowsCameraControls:NO];`. Here's another one: http://stackoverflow.com/questions/12313538/hide-record-button-in-uiimagepickercontroller. Best search for what you want on SO before posting ;) – brandonscript Dec 19 '13 at 23:39
  • The second one is good, but in the editor I get the error Unknown Type Name "myImagePicker" when I put the code into Xcode. How do I fix this. Also, how can I connect this code to my xib? @r3mus – Branch Dec 19 '13 at 23:44
  • Use AV Foundation example from Apple: https://developer.apple.com/library/ios/samplecode/avcam/Introduction/Intro.html – user523234 Dec 20 '13 at 01:28

1 Answers1

16

I don't know about a simple tutorial but adding a view that shows what the camera sees is super easy.

First:

Add a UIView to your interface builder that will be where the camera will be shown.

Second:

Add the AVFoundation framework to your project, and add its import to your ViewController .m file.

#import <AVFoundation/AVFoundation.h>

Third:

Add these 2 variables to your interface variable declarations

AVCaptureVideoPreviewLayer *_previewLayer;
AVCaptureSession *_captureSession;

Fourth:

Add this code to your viewDidLoad. (The explanation of what it does is commented)

//-- Setup Capture Session.
_captureSession = [[AVCaptureSession alloc] init];

//-- Creata a video device and input from that Device.  Add the input to the capture session.
AVCaptureDevice * videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if(videoDevice == nil)
    assert(0);

//-- Add the device to the session.
NSError *error;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice
                                                                    error:&error];
if(error)
    assert(0);

[_captureSession addInput:input];

//-- Configure the preview layer
_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[_previewLayer setFrame:CGRectMake(0, 0,
                                   self.cameraPreviewView.frame.size.width,
                                   self.cameraPreviewView.frame.size.height)];

//-- Add the layer to the view that should display the camera input
[self.cameraPreviewView.layer addSublayer:_previewLayer];

//-- Start the camera
[_captureSession startRunning];

Notes:

  1. The asserts will make the program exit in places where a camera is not available.

  2. This only shows "The preview" of what the camera sees, if you want to manipulate the input, or take a picture or record the video you need to configure additional things like the SessionPreset and add the corresponding capture delegates. But in that case you should follow a proper tutorial or read the documentation.

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • How can I take picture using this custom camera.? – Meet Doshi Jun 17 '16 at 10:30
  • you use the method that has this signature: - (void)captureStillImageAsynchronouslyFromConnection:(AVCaptureConnection *)connection completionHandler:(void (^)(CMSampleBufferRef imageDataSampleBuffer, NSError *error))handler; – Pochi Jun 20 '16 at 04:49