`
jsntghf
  • 浏览: 2532893 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

自定义UIImagePickerController的拍照页面

    博客分类:
  • iOS
阅读更多

主要是用到了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 使用相机拍照和自定义拍照界面

    在iOS应用开发中,利用相机功能为用户提供拍照和自定义拍照界面是常见的需求。这篇教程将深入探讨如何在iOS平台上实现这些功能。首先,我们从系统相机的使用开始。 一、使用系统相机 1. 请求权限:在iOS 10及以上...

    ios拍照后按范围裁剪图片demo

    首先,我们要创建一个自定义的拍照页面。这通常通过集成`UIImagePickerController`类来实现,它是苹果提供的用于拍摄照片或录制视频的UI组件。为了自定义这个页面,我们需要设置`UIImagePickerController`的代理,并...

    iOS 系统相册调取及自定义相册

    本示例项目"iOS 系统相册调取及自定义相册"旨在帮助开发者了解如何实现这一功能,同时提供对相册界面和相机页面进行自定义的可能性。通过深入理解以下知识点,你将能够构建自己的照片选择器并按照应用程序的视觉风格...

    ios相机和浏览器实现源码

    你可以设置相机模式(拍照或录像)、是否显示相册选项,以及处理捕获的图像或视频的数据回调。例如,你可以通过以下代码打开相机: ```swift let imagePickerController = UIImagePickerController() ...

    ios-模仿微信发朋友圈信息时,从相册或拍照选择图片,支持图片浏览和删除.zip

    总之,实现“ios-模仿微信发朋友圈信息时,从相册或拍照选择图片,支持图片浏览和删除”的功能,需要掌握多媒体选择、图片处理、手势识别、自定义视图和用户交互等多方面的iOS开发知识。通过以上步骤,你可以构建出...

    从相机获取图片以及微信界面的模仿UI特效

    而在iOS中,可以使用UIImagePickerController类来实现同样的功能,设置其sourceType为UIImagePickerControllerSourceTypeCamera,然后展示相机界面让用户拍照。 其次,模仿微信的UI特效主要涉及以下几个方面: 1. ...

    苹果动图滑动

    选择图片、拍照和视频上传的功能,这通常涉及到UIImagePickerController的使用。UIImagePickerController是苹果提供的一个控制器,用于让用户从相册中选择图片或视频,或者直接通过摄像头拍摄。开发者需要处理相应的...

    实现给照片和相机加上各种滤镜效果

    3. **自定义相机界面**:为了在拍照时实时预览并应用滤镜,开发者可能创建了一个自定义的相机界面。这通常涉及到捕获摄像头的实时视频流,将其转换为CIImage对象,然后应用滤镜,最后显示在屏幕上。这个过程需要对...

    底部弹出框

    在实现过程中,通常会调用系统提供的API,如Android的`Intent.ACTION_PICK`和`CameraSourcePreview`,或是iOS的`UIImagePickerController`,来打开相册或启动相机。 支付功能(如支付宝支付/微信支付)的集成则更...

Global site tag (gtag.js) - Google Analytics