简体   繁体   English

使用相机作为子视图

[英]use camera as subview

i'm using UIImagePickerControllerSourceTypeCamera in my app, but a want to add the camera view a subview. 我在我的应用程序中使用UIImagePickerControllerSourceTypeCamera,但想要将摄像机视图添加到子视图中。 not as modal view. 不是模态视图。

now i'm using 现在我正在使用

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];   

i want to use it like this, but doesn't seem to work .. 我想像这样使用它,但似乎不起作用..

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[picker.view setFrame:CGRectMake(300, 40, 300, 200)];
[self.view addSubview:picker.view];

i know that the UIImagePickerControllerSourceTypeCamera only can be used as modal view. 我知道UIImagePickerControllerSourceTypeCamera只能用作模态视图。 How is for example Hipstamatic using this for example? 例如Hipstamatic如何使用它?

This is possible in iOS 4+ using the AVCaptureSession framework . 这在使用AVCaptureSession框架的 iOS 4+中是可能的。 It's much more complicated than using UIImagePickerController, but you have complete control over input and output. 它比使用UIImagePickerController复杂得多,但您可以完全控制输入和输出。 The key to having the camera view as a subview is the following code. 将摄像机视图作为子视图的关键是以下代码。 Assume you have a subview called previewView that you want to show the camera view: 假设您有一个名为previewView的子视图,您想要显示摄像机视图:

AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.frame = self.previewView.bounds; 
[self.previewView.layer addSublayer:previewLayer];

There is more setup work to do to give your AVCaptureSession object the correct input and output devices and to actually capture the photos. 还有更多的设置工作要做,以使AVCaptureSession对象具有正确的输入和输出设备,并实际捕获照片。 That can be found in the documentation and examples on the web. 这可以在Web上的文档和示例中找到。

You're right, the only supported methods for displaying a UIImagePickerController seem to be presentModalViewController:animated: or UIPopoverController. 你是对的,显示UIImagePickerController的唯一受支持的方法似乎是presentModalViewController:animated:或UIPopoverController。

I'm not familiar with Hipstamatic, but chances are it is using AVFoundation , in particular AVCaptureVideoPreviewLayer . 我不熟悉Hipstamatic,但很可能是使用AVFoundation ,特别是AVCaptureVideoPreviewLayer

Visit link to get idea how things can be accomplished. 访问链接以了解如何完成任务。 Look out for AVCam Demo in developer.apple.com resources for IOS programme. 在developer.apple.com资源中查找IOS程序的AVCam Demo。 You will definitely get idea how it works. 你一定会知道它是如何工作的。 You should be registered developer with APPLE, and thats very easy. 您应该是APPLE的注册开发人员,这很容易。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM