簡體   English   中英

使用相機作為子視圖

[英]use camera as subview

我在我的應用程序中使用UIImagePickerControllerSourceTypeCamera,但想要將攝像機視圖添加到子視圖中。 不是模態視圖。

現在我正在使用

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

我想像這樣使用它,但似乎不起作用..

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

我知道UIImagePickerControllerSourceTypeCamera只能用作模態視圖。 例如Hipstamatic如何使用它?

這在使用AVCaptureSession框架的 iOS 4+中是可能的。 它比使用UIImagePickerController復雜得多,但您可以完全控制輸入和輸出。 將攝像機視圖作為子視圖的關鍵是以下代碼。 假設您有一個名為previewView的子視圖,您想要顯示攝像機視圖:

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

還有更多的設置工作要做,以使AVCaptureSession對象具有正確的輸入和輸出設備,並實際捕獲照片。 這可以在Web上的文檔和示例中找到。

你是對的,顯示UIImagePickerController的唯一受支持的方法似乎是presentModalViewController:animated:或UIPopoverController。

我不熟悉Hipstamatic,但很可能是使用AVFoundation ,特別是AVCaptureVideoPreviewLayer

訪問鏈接以了解如何完成任務。 在developer.apple.com資源中查找IOS程序的AVCam Demo。 你一定會知道它是如何工作的。 您應該是APPLE的注冊開發人員,這很容易。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM