Skip to content

Commit

Permalink
新增调用系统相机录制视频功能;支持导出指定尺寸的视频功能;支持给视频添加图片水印;优化部分UI显示;
Browse files Browse the repository at this point in the history
  • Loading branch information
longitachi committed Dec 21, 2017
1 parent 7325169 commit 86a9864
Show file tree
Hide file tree
Showing 15 changed files with 417 additions and 30 deletions.
2 changes: 2 additions & 0 deletions PhotoBrowser/ZLCustomCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

@property (nonatomic, assign) CFTimeInterval maxRecordTime;

//TODO: 添加是否允许拍照
@property (nonatomic, assign) BOOL allowTakePhoto;
//是否允许录制视频
@property (nonatomic, assign) BOOL allowRecordVideo;

Expand Down
27 changes: 20 additions & 7 deletions PhotoBrowser/ZLCustomCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ @interface CameraToolView : UIView <CAAnimationDelegate, UIGestureRecognizerDele

@property (nonatomic, weak) id<CameraToolViewDelegate> delegate;

@property (nonatomic, assign) BOOL allowTakePhoto;
@property (nonatomic, assign) BOOL allowRecordVideo;
@property (nonatomic, strong) UIColor *circleProgressColor;
@property (nonatomic, assign) NSInteger maxRecordDuration;
Expand Down Expand Up @@ -119,6 +120,15 @@ - (void)layoutSubviews
self.doneBtn.layer.cornerRadius = height*kBottomViewScale/2;
}

- (void)setAllowTakePhoto:(BOOL)allowTakePhoto
{
_allowTakePhoto = allowTakePhoto;
if (allowTakePhoto) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.bottomView addGestureRecognizer:tap];
}
}

- (void)setAllowRecordVideo:(BOOL)allowRecordVideo
{
_allowRecordVideo = allowRecordVideo;
Expand All @@ -143,9 +153,6 @@ - (void)setupUI
self.topView.userInteractionEnabled = NO;
[self addSubview:self.topView];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.bottomView addGestureRecognizer:tap];

self.dismissBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.dismissBtn.frame = CGRectMake(60, self.bounds.size.height/2-25/2, 25, 25);
[self.dismissBtn setImage:GetImageWithName(@"arrow_down") forState:UIControlStateNormal];
Expand Down Expand Up @@ -459,6 +466,9 @@ - (void)viewDidAppear:(BOOL)animated
[UIApplication sharedApplication].statusBarHidden = YES;
[self.session startRunning];
[self setFocusCursorWithPoint:self.view.center];
if (!self.allowTakePhoto && !self.allowRecordVideo) {
ShowAlert(@"allowTakePhoto与allowRecordVideo不能同时为NO", self);
}
}

- (void)viewDidDisappear:(BOOL)animated
Expand Down Expand Up @@ -499,6 +509,7 @@ - (void)setupUI

self.toolView = [[CameraToolView alloc] init];
self.toolView.delegate = self;
self.toolView.allowTakePhoto = self.allowTakePhoto;
self.toolView.allowRecordVideo = self.allowRecordVideo;
self.toolView.circleProgressColor = self.circleProgressColor;
self.toolView.maxRecordDuration = self.maxRecordDuration;
Expand Down Expand Up @@ -846,10 +857,12 @@ - (void)captureOutput:(AVCaptureFileOutput *)output didStartRecordingToOutputFil
- (void)captureOutput:(AVCaptureFileOutput *)output didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray<AVCaptureConnection *> *)connections error:(NSError *)error
{
if (CMTimeGetSeconds(output.recordedDuration) < 1) {
//视频长度小于1s 则拍照
NSLog(@"视频长度小于0.5s,按拍照处理");
[self onTakePicture];
return;
if (self.allowTakePhoto) {
//视频长度小于1s 允许拍照则拍照,不允许拍照,则保存小于1s的视频
NSLog(@"视频长度小于1s,按拍照处理");
[self onTakePicture];
return;
}
}

self.videoUrl = outputFileURL;
Expand Down
12 changes: 12 additions & 0 deletions PhotoBrowser/ZLDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "NSBundle+ZLPhotoBrowser.h"

#define ZLPhotoBrowserCameraText @"ZLPhotoBrowserCameraText"
#define ZLPhotoBrowserCameraRecordText @"ZLPhotoBrowserCameraRecordText"
#define ZLPhotoBrowserAblumText @"ZLPhotoBrowserAblumText"
#define ZLPhotoBrowserCancelText @"ZLPhotoBrowserCancelText"
#define ZLPhotoBrowserOriginalText @"ZLPhotoBrowserOriginalText"
Expand Down Expand Up @@ -107,6 +108,7 @@ typedef NS_ENUM(NSUInteger, ZLLanguageType) {
ZLLanguageJapanese,
};

//录制视频及拍照分辨率
typedef NS_ENUM(NSUInteger, ZLCaptureSessionPreset) {
ZLCaptureSessionPreset325x288,
ZLCaptureSessionPreset640x480,
Expand All @@ -115,12 +117,22 @@ typedef NS_ENUM(NSUInteger, ZLCaptureSessionPreset) {
ZLCaptureSessionPreset3840x2160,
};

//导出视频类型
typedef NS_ENUM(NSUInteger, ZLExportVideoType) {
//default
ZLExportVideoTypeMov,
ZLExportVideoTypeMp4,
};

//导出视频水印位置
typedef NS_ENUM(NSUInteger, ZLWatermarkLocation) {
ZLWatermarkLocationTopLeft,
ZLWatermarkLocationTopRight,
ZLWatermarkLocationCenter,
ZLWatermarkLocationBottomLeft,
ZLWatermarkLocationBottomRight,
};

static inline void SetViewWidth(UIView *view, CGFloat width) {
CGRect frame = view.frame;
frame.size.width = width;
Expand Down
28 changes: 24 additions & 4 deletions PhotoBrowser/ZLPhotoActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "ZLEditVideoController.h"
#import "ZLCustomCamera.h"
#import "ZLDefine.h"
#import <MobileCoreServices/MobileCoreServices.h>

#define kBaseViewHeight (self.configuration.maxPreviewCount ? 300 : 142)

Expand Down Expand Up @@ -137,7 +138,11 @@ - (instancetype)init
- (void)layoutSubviews
{
[super layoutSubviews];
[self.btnCamera setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserCameraText) forState:UIControlStateNormal];
if (!self.configuration.allowSelectImage && self.configuration.allowRecordVideo) {
[self.btnCamera setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserCameraRecordText) forState:UIControlStateNormal];
} else {
[self.btnCamera setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserCameraText) forState:UIControlStateNormal];
}
[self.btnAblum setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserAblumText) forState:UIControlStateNormal];
[self.btnCancel setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserCancelText) forState:UIControlStateNormal];
[self resetSubViewState];
Expand Down Expand Up @@ -452,15 +457,28 @@ - (IBAction)btnCamera_Click:(id)sender
[self hide];
return;
}
if (!self.configuration.allowSelectImage &&
!self.configuration.allowRecordVideo) {
ShowAlert(@"allowSelectImage与allowRecordVideo不能同时为NO", self.sender);
return;
}
if (self.configuration.useSystemCamera) {
//系统相机拍照
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.videoQuality = UIImagePickerControllerQualityTypeLow;
picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
NSArray *a1 = self.configuration.allowSelectImage?@[(NSString *)kUTTypeImage]:@[];
NSArray *a2 = self.configuration.allowRecordVideo?@[(NSString *)kUTTypeMovie]:@[];
NSMutableArray *arr = [NSMutableArray array];
[arr addObjectsFromArray:a1];
[arr addObjectsFromArray:a2];

picker.mediaTypes = arr;
picker.videoMaximumDuration = self.configuration.maxRecordDuration;
[self.sender showDetailViewController:picker sender:nil];
}
} else {
Expand All @@ -471,6 +489,7 @@ - (IBAction)btnCamera_Click:(id)sender
return;
}
ZLCustomCamera *camera = [[ZLCustomCamera alloc] init];
camera.allowTakePhoto = self.configuration.allowSelectImage;
camera.allowRecordVideo = self.configuration.allowRecordVideo;
camera.sessionPreset = self.configuration.sessionPreset;
camera.videoType = self.configuration.exportVideoType;
Expand Down Expand Up @@ -839,8 +858,9 @@ - (void)pushEditVideoVCWithModel:(ZLPhotoModel *)model
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self saveImage:image videoUrl:nil];
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
NSURL *url = [info valueForKey:UIImagePickerControllerMediaURL];
[self saveImage:image videoUrl:url];
}];
}

Expand Down
37 changes: 37 additions & 0 deletions PhotoBrowser/ZLPhotoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,43 @@
+ (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type presetName:(NSString *)presetName complete:(void (^)(NSString *exportFilePath, NSError *error))complete;


/**
导出指定尺寸的视频,视频区域为以视频中心为中点(视频质量未压缩)
@param asset 需要导出视频的asset
@param type 视频导出格式
@param renderSize 指定的尺寸大小
*/
+ (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type renderSize:(CGSize)renderSize complete:(void (^)(NSString *exportFilePath, NSError *error))complete;


/**
导出指定尺寸视频,并添加水印,视频区域为以视频中心为中点(视频质量未压缩)
@discussion(由于文字水印在开发过程中遇到对同一个视频导出时候,有的文字显示,有的不显示的文字,所以暂不支持文字水印)
@param asset 需要导出视频的asset
@param type 视频导出格式
@param renderSize 指定的尺寸大小,如要导出全尺寸视频,可将该值设置的大些如:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
@param watermarkImage 水印图片
@param location 水印位置
@param imageSize 水印大小
*/
+ (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type renderSize:(CGSize)renderSize watermarkImage:(UIImage *)watermarkImage watermarkLocation:(ZLWatermarkLocation)location imageSize:(CGSize)imageSize complete:(void (^)(NSString *exportFilePath, NSError *error))complete;


/**
导出全尺寸视频,并添加水印(支持设置压缩系数)
@param asset 需要导出视频的asset
@param type 视频导出格式
@param presetName 视频压缩设置
@param watermarkImage 水印图片
@param location 水印位置
@param imageSize 水印大小
*/
+ (void)exportVideoForAsset:(PHAsset *)asset type:(ZLExportVideoType)type presetName:(NSString *)presetName watermarkImage:(UIImage *)watermarkImage watermarkLocation:(ZLWatermarkLocation)location imageSize:(CGSize)imageSize complete:(void (^)(NSString *exportFilePath, NSError *error))complete;

/**
获取保存视频的路径
*/
Expand Down
Loading

0 comments on commit 86a9864

Please sign in to comment.