【问题标题】:camera overlay .xib file not showing buttons相机覆盖 .xib 文件不显示按钮
【发布时间】:2023-03-23 10:40:01
【问题描述】:

我正在尝试创建一个简单的相机应用程序,其覆盖视图显示两个按钮和一个手势识别器。

目前我有一个带有 .xib 文件的“OverlayViewController”,其中包含一个带有两个按钮和一个手势识别器的 UIView。

在我的 CameraViewController 中,我通过 UIImagePicker 在 vi​​ewDidLoad 上显示相机,我尝试将此选择器分配给我的叠加层,但到目前为止我没有得到叠加层中的任何按钮....

OverlayViewController.h

@interface OverlayViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate>

- (IBAction)take_picture:(UIButton *)sender;
- (IBAction)choose_picture:(UIButton *)sender;

@end

OverlayViewController.m - 几乎是空的(我需要在这里初始化 UIButtons 吗??)

CameraViewController.h

@interface ProfileViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@property (strong, nonatomic) UIImagePickerController *picker;
@property (strong, nonatomic) OverlayViewController *overlay;

CameraViewController.m

-(void)setupCamera
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

self.overlay = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
  // _overlay.delegate = self;


//    NSArray* nibViews= [[NSBundle mainBundle] loadNibNamed:@"OverlayViewController" owner:self.overlay options:nil];
//    UIView* uiView= [nibViews objectAtIndex:0];
//    uiView.opaque= NO;
//    uiView.backgroundColor= [UIColor clearColor];

self.picker.cameraOverlayView = self.overlay.view;
self.picker.delegate = self.overlay;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = YES;

[self presentViewController:self.picker animated:NO completion:nil];

}

我也想知道哪个控制器应该是这些不可见按钮的代表...

提前感谢您的帮助!

【问题讨论】:

    标签: ios overlay uiimagepickercontroller camera-overlay


    【解决方案1】:

    还有另一种选择AVCaptureSession 使用它你可以做你的事..

    将此代码放入viewDidAppear

    //----- SHOW LIVE CAMERA PREVIEW -----
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPreset1920x1080;
    
    CALayer *viewLayer = self.overlay.layer;
    NSLog(@"viewLayer = %@", viewLayer);
    
    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    
    [[captureVideoPreviewLayer connection] setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
    
    captureVideoPreviewLayer.frame = self.overlay.bounds;
    [self.overlay.layer addSublayer:captureVideoPreviewLayer];
    
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];
    
    [session startRunning];
    

    用于捕获图像

    -(UIImage *) captureNow
    {
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }
    
    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection 
                                                  completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
    {
        CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
        if (exifAttachments)
        {
            // Do something with the attachments.
            NSLog(@"attachements: %@", exifAttachments);
        }
        else 
            NSLog(@"no attachments");
    
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];
    
        return image;
    }];
    }
    

    这里是参考link

    【讨论】:

    • 感谢您的帮助,但是我没有看到您在哪里使用 ---CALayer *viewLayer = self.overlay.layer; ----我猜这个viewLayer将是我存储在CameraViewController中作为self.overlay的OverlayViewController层但是我不必将它设置为captureVideoPreviewLayer的覆盖层吗??
    • 我也遇到了一个问题,即 self.overlay 没有属性层......你在 ---CALayer *viewLayer = self.overlay 行中到底是什么意思。层; ---
    • 你添加了#import 吗?
    • 如果您没有在项目中添加 QuartzCore.framework,请先添加此框架,然后添加 #import 。而“叠加”是您对添加按钮的看法。
    猜你喜欢
    • 1970-01-01
    • 2015-05-28
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2022-06-23
    相关资源
    最近更新 更多