iOS个人修养iOS音视频iOS开发

AVCaptureSession自定义相机

2015-12-01  本文已影响1948人  CGPointZero

说明:
一般情况下,我们采用UIImagePickerController来调用系统提供的相机来拍照,非常好用。但是有时UIImagePickerController控件无法满足我们的需求,例如我们需要更加复杂的相机界面,这时候我们就要自定义相机了。
本文用的是AVFoundation框架做的一个自定义相机demo,包含了基本的对焦以及焦距调整功能。因为自己要用到,所以写了这样一个Demo,方便有同样需求的人查阅。直接上代码:
<pre>#import "ViewController.h"

import <AVFoundation/AVFoundation.h>

import <CoreGraphics/CoreGraphics.h>

import <QuartzCore/QuartzCore.h>

define kWidth ([UIScreen mainScreen].bounds.size.width)

define kHeight ([UIScreen mainScreen].bounds.size.height)

@interface ViewController ()

@property(nonatomic,strong)AVCaptureSession *session;
@property(nonatomic,strong)AVCaptureDeviceInput *input;
@property(nonatomic,strong)AVCaptureStillImageOutput *output;
@property(nonatomic,strong)AVCaptureVideoPreviewLayer *previewLayer;
//拍照
@property(nonatomic,strong)UIButton *shutterBtn;
//对焦
@property(nonatomic,strong)UIView *focalReticule;
//焦距Button
@property(nonatomic,strong)UIButton *focalBtn;

@end

//焦距
static float kCameraScale=1.0;

@implementation ViewController

}
//取消
-(void)cancel:(UIButton *)sender
{
[sender.superview removeFromSuperview];
}
//保存
-(void)save:(UIButton *)sender
{
UIImageView *imv=(UIImageView *)sender.superview;
//保存到相册
UIImageWriteToSavedPhotosAlbum(imv.image, nil, nil, nil);
[imv removeFromSuperview];
}
//播放拍照音效
-(void)shutterSuccessAlert
{
//播放音效
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound.mp3" ofType:nil]],&soundID);
//播放短音频
AudioServicesPlaySystemSound(soundID);
//增加震动效果
//AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
//隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
return YES;
}

@end</pre>
<pre>
附上:GitHub代码地址,希望能多多支持!</pre>

上一篇下一篇

猜你喜欢

热点阅读