1

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

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

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

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

}

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

我在这里先向您的帮助表示感谢!

4

1 回答 1

0

There is another alternative AVCaptureSession Using it you can do your things..

put this code in 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];

and for capture image

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

here is reference link

于 2013-07-25T10:02:07.097 回答