開始時准備封裝成ViewController的相機,但是在不改我相機控件源碼的情況下,使用者很難自定義UI。於是想到將相機核心功能封裝到View里,暴露出功能方法給外面調用,調用者只需將LFCamera貼到自己的控制器里即可,相機的UI由使用者在自己的控制器里畫。
一、使用方法
在你的控制器中加上相機只需如下三行代碼即可
- (void)viewDidLoad { [super viewDidLoad]; self.lfCamera = [[LFCamera alloc] initWithFrame:self.view.bounds]; //設置有效區域(可不設置,不設置則不顯示遮罩層和邊框) self.lfCamera.effectiveRect = CGRectMake(20, 200, self.view.frame.size.width - 40, 280); [self.view insertSubview:self.lfCamera atIndex:0]; }
然后使用者只需在自己的控制器添加一些按鈕如:拍照、閃光燈開關、切換前后攝像頭等按鈕,按鈕的事件調用LFCamera里的一些方法即可。
二、功能
看看.h文件就知道有哪些功能了,
//閃光燈模式 typedef NS_ENUM(NSInteger, LFCaptureFlashMode) { LFCaptureFlashModeOff = 0, LFCaptureFlashModeOn = 1, LFCaptureFlashModeAuto = 2 }; @interface LFCamera : UIView @property (assign, nonatomic) CGRect effectiveRect;//拍攝有效區域(可不設置,不設置則不顯示遮罩層和邊框) //有效區邊框色,默認橘色 @property (nonatomic, strong) UIColor *effectiveRectBorderColor; //遮罩層顏色,默認黑色半透明 @property (nonatomic, strong) UIColor *maskColor; @property (nonatomic) UIView *focusView;//聚焦的view - (instancetype)initWithFrame:(CGRect)frame; /**獲取攝像頭方向*/ - (BOOL)isCameraFront; /**獲取閃光燈模式*/ - (LFCaptureFlashMode)getCaptureFlashMode; /**切換閃光燈*/ - (void)switchLight:(LFCaptureFlashMode)flashMode; /**切換攝像頭*/ - (void)switchCamera:(BOOL)isFront; /**拍照*/ - (void)takePhoto:(void (^)(UIImage *img))resultBlock; /**重拍*/ - (void)restart;
源碼地址:https://github.com/zhanglinfeng/LFCamera
2017.12.25修改:
這只是LFKit的一個子庫,LFKit地址https://github.com/zhanglinfeng/LFKit
以后只在LFKit中維護了
只需LFCamera的 pod 'LFKit/Component/LFCamera'
需要LFKit中所有自定義控件的pod 'LFKit/Component'
需要總庫的 pod 'LFKit'
需要總庫的 pod 'LFKit'