liv a
liv a

Reputation: 3340

open the camera in my own uiview without opening the camera application

My Iphone application uses the camera, I have the following code:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;

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

I wish that the camera application will not open and instead i'll have the camera feed in my own uiview or uiimageview is that possible?

I've tried this line of code instead of [self presentViewController:picker animated:YES completion:NULL];:

[self.view addSubview:picker];

this results in an exception [UIImagePickerController superview]: unrecognized selector sent to instance

Solution: I found this great tutorial including a code sample and a download project

Upvotes: 0

Views: 2929

Answers (3)

iOS Dev
iOS Dev

Reputation: 4248

You can achieve this using the following code:

#import <AVFoundation/AVFoundation.h>

AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice)
{
    NSError *error;
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
    if (!error)
    {
        if ([session canAddInput:videoInput])
        {
            [session addInput:videoInput];
            AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
            previewLayer.frame = self.view.bounds;
            [self.view.layer addSublayer:previewLayer];
            [session startRunning];
        }
    }
}

Upvotes: 8

Srinivas
Srinivas

Reputation: 23

Yes.Do check Avfoundation framework ....You can use AVCaptureVideoPreviewLayer for having your own camera feed..

Check the following Apple sample code:

https://developer.apple.com/library/ios/samplecode/squarecam/Introduction/Intro.html

Upvotes: 0

Shaunti Fondrisi
Shaunti Fondrisi

Reputation: 1091

This can be done with UIImagePickerController. You need to add the view of the picker as a subview.

[self.view addSubview:picker.view];

Upvotes: 1

Related Questions