在不打开相机应用程序的情况下,在我自己的uiview中打开相机

时间:2014-01-02 19:00:53

标签: iphone objective-c camera uiimagepickercontroller

我的Iphone应用程序使用相机,我有以下代码:

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

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

我希望相机应用程序不会打开,而我会在我自己的uiview或者uiimageview中使用相机提示吗?

我尝试过这行代码而不是[self presentViewController:picker animated:YES completion:NULL];

[self.view addSubview:picker];

这导致异常[UIImagePickerController superview]: unrecognized selector sent to instance

解决方案: 我发现this great tutorial包括代码示例和下载项目

3 个答案:

答案 0 :(得分:8)

您可以使用以下代码实现此目的:

#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];
        }
    }
}

答案 1 :(得分:1)

这可以通过UIImagePickerController完成。您需要将选择器的视图添加为子视图。

[self.view addSubview:picker.view];

答案 2 :(得分:0)

是。请检查Avfoundation框架....您可以使用AVCaptureVideoPreviewLayer拥有自己的相机Feed ..

检查以下Apple示例代码:

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