我正在尝试使用覆盖视图创建一个简单的相机应用程序,该应用程序显示两个按钮和一个手势识别器。
目前我有一个带有.xib文件的'OverlayViewController',其中包含带有两个按钮和手势识别器的UIView。
在我的CameraViewController中,我通过UIImagePicker在viewDidLoad上显示相机,我尝试将此选择器分配给我的叠加层,但到目前为止我没有获得叠加层中的任何按钮....
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];
}
我也想知道哪个控制器应该是这些隐形按钮的代表...
感谢您的帮助!
答案 0 :(得分:0)
还有另一种选择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