11

我有一个使用UIImagePickerController拍照的应用程序。问题是我只希望相机选项可用,并且我知道我需要隐藏标准控件:

cameraUI.showsCameraControls=NO;

并使用 acameraOverlayView来提供我自己的控件。我已经看过 Apple 的PhotoPicker项目,我最初的问题是如何将Overlay对象放到我的故事板上?我在图书馆找不到这样的对象。

4

3 回答 3

16

这是代码:

toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-54, self.view.frame.size.width, 55)];

toolBar.barStyle =  UIBarStyleBlackOpaque;
NSArray *items=[NSArray arrayWithObjects:
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel  target:self action:@selector(cancelPicture)],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera  target:self action:@selector(shootPicture)],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                nil];
[toolBar setItems:items];

// create the overlay view
overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];

// important - it needs to be transparent so the camera preview shows through!
overlayView.opaque=NO;
overlayView.backgroundColor=[UIColor clearColor];

// parent view for our overlay
UIView *cameraView=[[UIView alloc] initWithFrame:self.view.bounds];
[cameraView addSubview:overlayView];
[cameraView addSubview:toolBar];

imagePickerController = [[UIImagePickerController alloc] init];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO){
    NSLog(@"Camera not available");
    return;
}
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;

// hide the camera controls
imagePickerController.showsCameraControls=NO;
imagePickerController.wantsFullScreenLayout = YES;
[imagePickerController setCameraOverlayView:cameraView];

[self presentViewController:imagePickerController animated:YES completion:nil];

在头文件中声明:

UIImagePickerController * imagePickerController;
UIToolbar *toolBar;
OverlayView *overlayView;

从 Apples PhotoPicker 添加这个 OverlayView.h 和 .m 类。

使用自定义相机按钮拍摄照片的操作:

-(void) shootPicture {
    [imagePickerController takePicture];
}

- (IBAction)cancelPicture {
    [self dismissViewControllerAnimated:YES completion:nil];
}

输出将如下图所示(我在自定义叠加视图中添加了一个捕获按钮和取消按钮):

在此处输入图像描述

快乐编码:)

于 2014-01-19T05:27:19.010 回答
1

这是我的代码。如果您希望相机在视图控制器打开后立即出现,请确保像我一样在 viewDidAppear 中初始化 UIImagePickerController(viewDidLoad 不起作用)。

@interface CameraViewController ()
@property UIImagePickerController *PickerController;
@property CGFloat HeightOfButtons;
@end


- (UIView *)createCustomOverlayView
{

    // Main overlay view created
    UIView *main_overlay_view = [[UIView alloc] initWithFrame:self.view.bounds];

    // Clear view (live camera feed) created and added to main overlay view
    // ------------------------------------------------------------------------
    UIView *clear_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.HeightOfButtons)];
    clear_view.opaque = NO;
    clear_view.backgroundColor = [UIColor clearColor];
    [main_overlay_view addSubview:clear_view];
    // ------------------------------------------------------------------------


    // Creates two red buttons on the bottom of the view (on top of the live camera feed)
    // Then adds the buttons to the main overlay view
    // You can, of course, customize these buttons however you want
    // ------------------------------------------------------------------------
    for(int i = 0; i < 2; i++) {
        self.HeightOfButtons = 100;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        // when a button is touched, UIImagePickerController snaps a picture
        [button addTarget:self action:@selector(testIfButtonResponds) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake( i * self.view.frame.size.width / 2, self.view.frame.size.height - self.HeightOfButtons, self.view.frame.size.width / 2, self.HeightOfButtons);
        [button setBackgroundColor:[UIColor redColor]];
        [main_overlay_view addSubview:button];
    }
    // ------------------------------------------------------------------------

    return main_overlay_view;
}

- (void)makeCustomCameraAppear
{
    self.PickerController = [[UIImagePickerController alloc] init];
    self.PickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.PickerController.showsCameraControls = NO;
    self.PickerController.delegate = self;

    UIView *overlay_view = [self createCustomOverlayView];
    [self.PickerController setCameraOverlayView:overlay_view];

    [self presentViewController:self.PickerController animated:YES completion:NULL];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self makeCustomCameraAppear];
}
于 2014-07-23T22:41:02.450 回答
0

很棒的信息。为 Raghu 所做的添加一点:

顶部:

UIImagePicker *_imagePicker;

//inside your take a picture method
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
toolBar.barStyle =  UIBarStyleBlackOpaque;
NSArray *items=[NSArray arrayWithObjects:
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel  target:self action:@selector(cancelPicker)],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera  target:self action:@selector(shootPicture)],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                nil];
[toolBar setItems:items];
toolBar.backgroundColor=[UIColor blackColor];



UIImageView *tp=[[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-100, self.view.frame.size.width, 100)];

tp.userInteractionEnabled=YES;
tp.backgroundColor=[UIColor blackColor];

[tp addSubview:toolBar]; //add the toolbar to the uiimageview

_imagePicker=[[UIImagePickerController alloc]init];
_imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
_imagePicker.delegate = self;
_imagePicker.showsCameraControls=NO;  //we will make custom controls.
[_imagePicker.view addSubview:tp];



-(void)cancelPicker{
//get rid of the image picker
[self dismissViewControllerAnimated:YES completion:nil]; //dismiss uiimagepicker
_imagePicker=nil;
}

//take the picture, it will then go directly to - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method
-(void)shootPicture{
[_imagePicker takePicture];
}
于 2015-03-30T03:53:41.060 回答