自定义UIImagePickerController的拍照页面

主要是用到了cameraOverlayView这个属性,我们只要自定义一个UIView即可,这大大简化了我们的工作,核心代码如下所示,其他的请参考附件。

 

OverlayViewController.h

 

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>

@protocol OverlayViewControllerDelegate;

@interface OverlayViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
    id <OverlayViewControllerDelegate> delegate;
}    

@property (nonatomic, assign) id <OverlayViewControllerDelegate> delegate;
@property (nonatomic, retain) UIImagePickerController *imagePickerController;

- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType;

@end

@protocol OverlayViewControllerDelegate
- (void)didTakePicture:(UIImage *)picture;
- (void)didFinishWithCamera;
@end
  

OverlayViewController.m

 

#import "OverlayViewController.h"

enum {
	kOneShot,
	kRepeatingShot
};

@interface OverlayViewController ( )

@property (assign) SystemSoundID tickSound;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *takePictureButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *startStopButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *timedButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *cancelButton;
@property (nonatomic, retain) NSTimer *tickTimer;
@property (nonatomic, retain) NSTimer *cameraTimer;

- (IBAction)done:(id)sender;
- (IBAction)takePhoto:(id)sender;
- (IBAction)startStop:(id)sender;
- (IBAction)timedTakePhoto:(id)sender;

@end

@implementation OverlayViewController

@synthesize delegate;

#pragma mark -
#pragma mark OverlayViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:
                                                    [[NSBundle mainBundle] pathForResource:@"tick" ofType:@"aiff"]], &_tickSound);
        self.imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
        self.imagePickerController.delegate = self;
    }
    return self;
}

- (void)viewDidUnload {
    self.takePictureButton = nil;
    self.startStopButton = nil;
    self.timedButton = nil;
    self.cancelButton = nil;
    self.cameraTimer = nil;
    
    [super viewDidUnload];
}

- (void)dealloc {
    [_takePictureButton release];
    [_startStopButton release];
    [_cancelButton release];
    [_timedButton release];
    [_imagePickerController release];
    AudioServicesDisposeSystemSoundID(_tickSound);
    [_cameraTimer release];
    [_tickTimer release];
    
    [super dealloc];
}

- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType {
    self.imagePickerController.sourceType = sourceType;
    
    if (sourceType == UIImagePickerControllerSourceTypeCamera) {
        self.imagePickerController.showsCameraControls = NO;
        
        if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0) {
            CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
            CGRect newFrame = CGRectMake(0.0,
                                         CGRectGetHeight(overlayViewFrame) -
                                         self.view.frame.size.height - 10.0,
                                         CGRectGetWidth(overlayViewFrame),
                                         self.view.frame.size.height + 10.0);
            self.view.frame = newFrame;
            [self.imagePickerController.cameraOverlayView addSubview:self.view];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    [self.cameraTimer invalidate];
    _cameraTimer = nil;
    [self.tickTimer invalidate];
    _tickTimer = nil;
}

- (void)finishAndUpdate {
    [self.delegate didFinishWithCamera];
    
    self.cancelButton.enabled = YES;
    self.takePictureButton.enabled = YES;
    self.timedButton.enabled = YES;
    self.startStopButton.enabled = YES;
    self.startStopButton.title = @"Start";
}

#pragma mark -
#pragma mark Camera Actions

- (IBAction)done:(id)sender {
    if (![self.cameraTimer isValid])
        [self finishAndUpdate];
}

- (IBAction)timedTakePhoto:(id)sender {
    self.cancelButton.enabled = NO;
    self.takePictureButton.enabled = NO;
    self.timedButton.enabled = NO;
    self.startStopButton.enabled = NO;
    
    if (self.cameraTimer != nil)
        [self.cameraTimer invalidate];
    _cameraTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                    target:self
                                                  selector:@selector(timedPhotoFire:)
                                                  userInfo:[NSNumber numberWithInt:kOneShot]
                                                   repeats:YES];
    
    if (self.tickTimer != nil)
        [self.tickTimer invalidate];
    _tickTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(tickFire:)
                                                userInfo:nil
                                                 repeats:YES];
}

- (IBAction)takePhoto:(id)sender {
    [self.imagePickerController takePicture];
}

- (IBAction)startStop:(id)sender {
    if ([self.cameraTimer isValid]) {
        [self.cameraTimer invalidate];
        _cameraTimer = nil;
        [self finishAndUpdate];
    } else {
        self.startStopButton.title = @"Stop";
        self.cancelButton.enabled = NO;
        self.timedButton.enabled = NO;
        self.takePictureButton.enabled = NO;
        
        _cameraTimer = [NSTimer scheduledTimerWithTimeInterval:1.5
                                                        target:self
                                                      selector:@selector(timedPhotoFire:)
                                                      userInfo:[NSNumber numberWithInt:kRepeatingShot]
                                                       repeats:YES];
        [self.cameraTimer fire];
    }
}

#pragma mark -
#pragma mark Timer

- (void)timedPhotoFire:(NSTimer *)timer {
    [self.imagePickerController takePicture];
    
    NSInteger cameraAction = [self.cameraTimer.userInfo integerValue];
    switch (cameraAction) {
        case kOneShot: {
            [self.cameraTimer invalidate];
            _cameraTimer = nil;
            [self.tickTimer invalidate];
            _tickTimer = nil;
            
            break;
        }
            
        case kRepeatingShot: {
            break;
        }
    }
}

- (void)tickFire:(NSTimer *)timer {
	AudioServicesPlaySystemSound(self.tickSound);
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    
    if (self.delegate)
        [self.delegate didTakePicture:image];
    
    if (![self.cameraTimer isValid])
        [self finishAndUpdate];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self.delegate didFinishWithCamera];    
}

@end

你可能感兴趣的:(ios,iPhone)