Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在打开的摄像头上显示摄像头滚轮UIImagePickerController_Ios_Objective C_Uiimagepickercontroller - Fatal编程技术网

Ios 在打开的摄像头上显示摄像头滚轮UIImagePickerController

Ios 在打开的摄像头上显示摄像头滚轮UIImagePickerController,ios,objective-c,uiimagepickercontroller,Ios,Objective C,Uiimagepickercontroller,在我的应用程序中,我打开的相机上方有一个cameraOverlayView,带有相机按钮的自定义控件。该应用程序允许用户在关闭相机之前拍摄多张照片,因此快门按钮不会调用dismissViewControllerAnimated,而是有一个关闭按钮,用于在完成拍照时关闭 现在,相机覆盖上的一个按钮是gallery按钮,允许用户选择保存的图像,而不是拍摄新图像。我尝试了两种不同的方法来实现这一点,但都失败了 第一种方法 使用当前显示覆盖的同一UIImagePickerController实例,并将s

在我的应用程序中,我打开的相机上方有一个
cameraOverlayView
,带有相机按钮的自定义控件。该应用程序允许用户在关闭相机之前拍摄多张照片,因此快门按钮不会调用
dismissViewControllerAnimated
,而是有一个关闭按钮,用于在完成拍照时关闭

现在,相机覆盖上的一个按钮是gallery按钮,允许用户选择保存的图像,而不是拍摄新图像。我尝试了两种不同的方法来实现这一点,但都失败了

第一种方法

使用当前显示覆盖的同一
UIImagePickerController
实例,并将
sourceType
切换到库。当时它确实展示了画廊,但当点击一张照片时,我不能在不忽略整个覆盖层的情况下忽略厨房

第二种方法

创建一个单独的
UIImagePickerController
实例,将
sourceType
设置为gallery,并尝试调用
presentViewController
,然后调用失败并发出警告:

“警告:试图在上显示 谁的视图不在窗口中 等级制度!”


有人能解决这个问题吗?这可能吗?

您可以为自己的自定义图库视图编码以选择照片,然后将其添加到
cameraOverlayView
子视图层次结构中

GitHub上一定有开源项目,它展示了如何在某处创建这些视图。或者,如果你不想从头开始,我正好有你想要的东西


实际上,这是一个非常简单的过程—一个集合视图,其中包含一个由
AssetLibrary
框架支持的数据源。

第二种方法是正确的。 我没有看到您的代码,但我认为您的控制器层次结构与此类似:

主VC---当前--->摄像头VC

所以,如果你打电话

[self presentViewController:picker animated:YES completion:^{}];
在主VC中,您试图显示“隐藏”VC中的另一个VC(由摄影机VC覆盖)

关键是参考您的相机VC(我们称之为cameraVC),并从主VC执行类似操作:

 [cameraVC presentViewController:theOtherPicker animated:YES completion:^{}];

执行此操作时,“显示”操作由摄像头VC(可见)执行,没有警告,而不是由隐藏的主VC执行。

我将按如下方式设置捕获会话:

- (void)setupCaptureSession
{
    NSError* error = nil;

    // Create the session
    _captureSession = [[AVCaptureSession alloc] init];    
    _captureSession.sessionPreset = AVCaptureSessionPresetMedium;
    AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

    [_captureSession addInput:input];
    AVCaptureVideoDataOutput* output = [[AVCaptureVideoDataOutput alloc] init];
    [_captureSession addOutput:output];

    // Configure your output.
   dispatch_queue_t queue = dispatch_queue_create("myCameraOutputQueue", NULL);
   //If you want to sebsequently use the data, then implement the delegate.
   [output setSampleBufferDelegate:self queue:queue]; 
}
完成此操作后,可以按如下方式创建预览层:

_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
[_captureSession startRunning];
然后将预览层添加到视图中:

_myView.layer addSubLayer:_previewLayer];

现在您已经完成了此设置,我将添加一个自定义图像选择器,如此:

我提供了一个自定义摄影机视图控制器,在其上添加一个“SourceTypeCamera”UIImagePickerController作为包含的子视图控制器。我的自定义视图控制器有一个按钮,它依次添加另一个UIImagePickerController实例,这是一个“SourceTypePhotoLibrary”

最终将得到嵌套的视图控制器层次结构:

  • 根/主视图控制器-显示:
  • 我的自定义摄影机视图控制器–添加为子视图控制器:
  • 照相机或照片库UIImagePickerController
大概是这样的:

- (void)viewDidLoad { (id<UIImagePickerControllerDelegate,UINavigationControllerDelegate>)theDelegate {
    [super viewDidLoad];
    [self startImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera
                                delegate:self];
    // configure button in camera overlay to call -pickPhotoTapped
}

- (void) pickPhotoTapped {
    [self startImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary
                                delegate:self];
}

- (BOOL) startImagePickerWithSourceType:(UIImagePickerControllerSourceType)sourceType
                               delegate:(id<UIImagePickerControllerDelegate,UINavigationControllerDelegate>)theDelegate {
    if (([UIImagePickerController isSourceTypeAvailable:sourceType] == NO)
            || (theDelegate == nil))
        return NO;

    self.cameraUI = [[UIImagePickerController alloc] init];
    self.cameraUI.sourceType = sourceType;
    self.cameraUI.view.frame = self.view.bounds;
    self.cameraUI.delegate = theDelegate;

    if (sourceType == UIImagePickerControllerSourceTypeCamera) {
        self.cameraUI.allowsEditing = NO;
        self.cameraUI.showsCameraControls = NO;
        self.cameraUI.cameraOverlayView = [self overlayView];
    }

    [self addChildViewController:self.cameraUI];
    [self.view addSubview:self.cameraUI.view];
    [self.cameraUI didMoveToParentViewController:self];

    return YES;
}
-(void)viewDidLoad{(id)删除门{
[超级视图下载];
[自启动TimagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera
代表:自我];
//配置摄像头覆盖中的按钮以调用-pickPhotoTapped
}
-(无效)扒手{
[自启动TimagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary
代表:自我];
}
-(BOOL)startImagePickerWithSourceType:(UIImagePickerControllerSourceType)sourceType
委托人:(id)委托人{
如果([UIImagePickerController IsSourceType可用:sourceType]==否)
||(德尔盖特==零)
返回否;
self.cameraUI=[[UIImagePickerController alloc]init];
self.cameraUI.sourceType=sourceType;
self.cameraUI.view.frame=self.view.bounds;
self.cameraUI.delegate=删除门;
if(sourceType==UIImagePickerControllerSourceTypeCamera){
self.cameraUI.allowsdediting=否;
self.cameraUI.showsCameraControls=否;
self.cameraUI.cameraOverlayView=[self-overlayView];
}
[self addChildViewController:self.cameraUI];
[self.view addSubview:self.cameraUI.view];
[self.cameraUI didMoveToParentViewController:self];
返回YES;
}

试试这个~~我想这是你的目标

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIImagePickerController *imagePicker;

@end

@implementation ViewController

@synthesize imagePicker = _imagePicker;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];

    sleep(2);

    _imagePicker = [[UIImagePickerController alloc] init];
    [_imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [_imagePicker setDelegate:self];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 100, 30)];
    [button setTitle:@"Library" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor darkGrayColor]];
    [button addTarget:self action:@selector(gotoLibrary:) forControlEvents:UIControlEventTouchUpInside];

    [_imagePicker.view addSubview:button];

    [self presentViewController:_imagePicker animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)gotoLibrary:(id)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    [imagePicker.view setFrame:CGRectMake(0, 80, 320, 350)];
    [imagePicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    [imagePicker setDelegate:self];

    [_imagePicker presentViewController:imagePicker animated:YES completion:nil];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end

是的,这是可能的。你能给我们看一下你的第二种方法的代码吗?我想你可能在错误的地方调用了它。看看这个:我尝试了第二种方法。它很好。我想你可以检查你的源代码。也许这就是Enrico Susatyo评论的问题。祝你好运~~这个自定义图像采集器以模态的形式出现,我已经有了e一个开放式相机模式,我不认为这会起作用。它不一定是模式,我相信你可以用所有的源代码来解决这个问题。顺便说一下:这是gitbub上的许多模式之一。我选择它是因为它包含图片…我说了,乍一看,这里似乎有一些其他好的答案…这个错误我在我的问题中提到,如果我尝试将gallery ImagePicker呈现在摄影机ImagePicker上,则会发生这种情况:
code
[自我呈现视图控制器:galleryImagePicker动画:是完成:无];您的控件是否显示为模态?是的,您将其包装在导航控制器中,然后以模态显示。我的问题是,我想在摄影机上显示图库,因此必须同时显示两个而不是两个UIImagePickerController。尝试使用此方法,但实例化第二个UIImagePickerController实例并添加它将现有UIImagePickerController实例作为子视图控制器。如果您想要像从底部向上滑动这样的过渡,请设置帧更改动画或使用+[UIView t]