UIImagePickerController覆盖takePicture方法

时间:2014-08-19 19:27:49

标签: ios uiimagepickercontroller

我通过执行以下操作使用xib文件调用相机覆盖:

Camera.h

#import <UIKit/UIKit.h>
#import "ControlsViewController.h"

@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,ProcessDataDelegate>

@property (nonatomic, strong) UIImagePickerController *imagePicker;

@end

Camera.m

#import "CameraViewController.h"
#import "ControlsViewController.h"

@interface CameraViewController ()

@end

@implementation CameraViewController

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


    //Get the camera
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
    self.imagePicker.showsCameraControls = NO;



    //set our custom overlay view
    ControlsViewController *overlayViewController = [[ControlsViewController alloc] initWithNibName:@"ControlsViewController" bundle:nil];
    [self addChildViewController:overlayViewController];
    self.imagePicker.cameraOverlayView = overlayViewController.view;

    [self presentViewController:self.imagePicker animated:NO completion:NULL];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)didClickCameraButton {
    [self.imagePicker takePicture];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

这会调用ControlsViewController的xib和.m .h文件。在这些文件中,我有一个用于拍照但不确定如何在叠加层中调用该方法的按钮。我想的是:

Overlay.H

#import <UIKit/UIKit.h>

@protocol ProcessDataDelegate <NSObject>


@end

@interface ControlsViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

{
    id <ProcessDataDelegate> delegate;
}


- (IBAction)cameraButton:(id)sender;

- (IBAction)kennedyButton:(id)sender;


@property (retain) id delegate;
-(void)didClickCameraButton;


@end

Overlay.M

#import "ControlsViewController.h"
#import "CameraViewController.h"

@interface ControlsViewController ()

@end

@implementation ControlsViewController

@synthesize delegate;

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

     [self.view setBackgroundColor:[UIColor clearColor]];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



- (IBAction)cameraButton:(id)sender {

    NSLog(@"this button works");
    [self.delegate didClickCameraButton];


}

- (IBAction)kennedyButton:(id)sender {

     NSLog(@"this button works");
}
@end

但很明显,imagePicker属性未通过覆盖.m文件传输。有谁知道如何去做?我在覆盖.m文件中导入了相机.H文件,但没有运气。谢谢。

1 个答案:

答案 0 :(得分:0)

在你的overlayViewController中,链接到cameraButton的按钮是什么?你有几层控件,最终你想要主控制器(你正在显示viewDidLoad的那个)来调用[self.imagePicker takePicture]。您可以向overlayViewController添加委托,这样当调用cameraButton:时,它只会告诉委托。

显然,您必须创建协议,设置代理等。

编辑:代表的通常模式是委托是弱的。如果您使用arc,则可能必须使overlayController不是瞬态的。

ControlsViewController.h

@interface ControlsViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    // don't need the delegate declared in here
}
@property (weak) id delegate;

ControlsViewController.m

- (IBAction)cameraButton:(id)sender {

    NSLog(@"this button works");

    [self.delegate didClickCameraButton];
}

YourMainViewController.h

@interface YourMainViewController : UIViewController <ControlsViewDelegate>
{
    ControlsViewController *overlayController;
}

YourMainViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    ...

    overlayViewController = [[ControlsViewController alloc] initWithNibName:@"ControlsViewController" bundle:nil];
    overlayViewController.delegate = self;
    self.imagePicker.cameraOverlayView = overlayViewController.view;

    [self presentViewController:self.imagePicker animated:NO completion:NULL];
}

-(void)didClickCameraButton {
    [self.picker takePicture];
}