1. 程式人生 > >iOS自定義拍攝小視訊壓縮上傳

iOS自定義拍攝小視訊壓縮上傳

//

//  LittleVideoViewController.h

//  uploadVideoDemo

//

//  Created by 歐陽榮 on 16/9/5.

//  Copyright © 2016 HengTaiXin. All rights reserved.

//

#import <UIKit/UIKit.h>

@protocol LittleVideoDelegate <NSObject>

- (void)finishLittleVideoViewControllerCapture:(NSURL

*)filePath;

@end

@interface LittleVideoViewController : UIViewController

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

@end

//

//  LittleVideoViewController.m

//  uploadVideoDemo

//

//  Created by 歐陽榮 on 16/9/5.

//  Copyright © 2016 HengTaiXin. All rights reserved.

//

#import "LittleVideoViewController.h"

#import <AVFoundation/AVFoundation.h>

#import "UIView+RMAdditions.h"

#define BLUECOLOR [UIColor colorWithRed:0/255.0 green:155/255.0 blue:225/255.0 alpha:1]

#define REDCOLOR [UIColor colorWithRed:255/255.0 green:27/255.0 blue:86/255.0 alpha:

1]

#define kDuration 8.0

#define kTrans SCREEN_WIDTH/kDuration/60.0

typedefNS_ENUM(NSInteger,VideoStatus){

    VideoStatusEnded = 0,

    VideoStatusStarted

};

@interface LittleVideoViewController ()<AVCaptureFileOutputRecordingDelegate>

{

    AVCaptureSession * _captureSession;

    AVCaptureDevice *_videoDevice;

    AVCaptureDevice *_audioDevice;

    AVCaptureDeviceInput *_videoInput;

    AVCaptureDeviceInput *_audioInput;

    AVCaptureMovieFileOutput *_movieOutput;

    AVCaptureVideoPreviewLayer *_captureVideoPreviewLayer;

}

@property (nonatomic,strong) UIView * navView;

@property (nonatomic,strong) UIButton * backBtn;

@property (nonatomic,strong) UIView * videoView;

@property (nonatomic,strong) UIView * bottomView;

@property (nonatomic,strong) UILabel * tapBtn;

@property (nonatomic,assign) VideoStatus status;

@property (nonatomic,strong) NSLayoutConstraint * progressWidth;

@property (nonatomic,strong) UIView *progressView;

@property (nonatomic,strong) CADisplayLink *link;

@property (nonatomic,assign) BOOL canSave;

@property (nonatomic,strong) UILabel * cancelTip;

@property (nonatomic,strong) UIView * focusCircle;

@property (nonatomic,strong) UIButton *changeBtn;

@property (nonatomic,strong) UIButton *flashModelBtn;

@end

@implementation LittleVideoViewController

- (void)viewDidLoad {

    [super viewDidLoad];

// Do any additional setup after loading the view.

    [self creatNavView];

}

#pragma mark - CreatUI

-(void)creatNavView{

self.videoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    [self.view addSubview:self.videoView];

self.videoView.layer.masksToBounds = YES;

self.navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 64)];

self.navView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.6];

    [self.view addSubview:self.navView];

    [self.navView addSubview:self.flashModelBtn];

    [self.navView addSubview:self.changeBtn];

self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];

//    [self.backBtn setTitle:@"取消" forState:UIControlStateNormal];

    [_backBtn setImage:[UIImage imageNamed:@"WechatShortVideo_close"] forState:UIControlStateNormal];

self.backBtn.frame = CGRectMake(15,25, 25, 25);

    [self.backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.navView addSubview:self.backBtn];

self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 170/2 - 4, SCREEN_WIDTH, 170/2 + 4)];

self.bottomView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.6];

    [self.view addSubview:self.bottomView];

self.tapBtn = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - 60/2, 85/2 - 60/2, 60, 60)];

self.tapBtn.text = @"按住拍";

self.tapBtn.textColor = [UIColor whiteColor];

    [self.bottomView addSubview:_tapBtn];

self.tapBtn.font = [UIFont systemFontOfSize:15];

self.tapBtn.textAlignment = NSTextAlignmentCenter;

    _tapBtn.layer.borderWidth = 4;

    _tapBtn.layer.cornerRadius = 60/2;

    _tapBtn.layer.masksToBounds = YES;

    _tapBtn.layer.borderColor = BLUECOLOR.CGColor;

//進度條

self.progressView = [[UIView alloc]init];

    _progressView.translatesAutoresizingMaskIntoConstraints = NO;

    _progressView.backgroundColor = BLUECOLOR;

self.progressView.alpha = 0;

    [self.view addSubview:_progressView];

//寬度先設定為

//view的中心橫座標等於父view的中心橫座標

    NSLayoutConstraint *constrant1 = [NSLayoutConstraint constraintWithItem:_progressView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];

//view的中心縱座標等於父view的中心縱座標

    NSLayoutConstraint *constrant2 = [NSLayoutConstraint constraintWithItem:_progressView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:SCREEN_HEIGHT - 170/2 -2 - SCREEN_HEIGHT/2];

self.progressWidth = [NSLayoutConstraint constraintWithItem:_progressView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute  multiplier:1.0 constant:SCREEN_WIDTH];

//view的高度為4

    NSLayoutConstraint *constrant4 = [NSLayoutConstraint constraintWithItem:_progressView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:4];

    NSArray *array = [NSArray arrayWithObjects:constrant1, constrant2, self.progressWidth, constrant4,nil];

    [self.view addConstraints:array];

    [self getAuthorization];

    [self addGenstureRecognizer];

}

#pragma mark touchs

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

NSLog(@"touch");

UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self.view];

BOOL condition = [selfisInBtnRect:point];

if (condition) {

        [selfisFitCondition:condition];

        [selfstartAnimation];

self.changeBtn.hidden= self.flashModelBtn.hidden = YES;

    }

}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [supertouchesMoved:touches withEvent:event];

NSLog(@"touchesMoved");

UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self.view];

BOOL condition = [selfisInBtnRect:point];

    [selfisFitCondition:condition];

}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

NSLog(@"touchesEnded");

UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self.view];

BOOL condition = [selfisInBtnRect:point];

/*

結束時候咱們設定有兩種情況依然算錄製成功

     1.擡手時,錄製時長 > 1/3總時長

     2.錄製進度條完成時,就算手指超出按鈕範圍也算錄製成功 -- 此時 end 方法不會呼叫,因為使用者手指還在螢幕上,所以直接程式碼呼叫錄製成功的方法,將控制器切換

     */

if (condition) {

NSLog(@"手指還在按鈕範圍之內");

if (self.progressWidth.constant < SCREEN_WIDTH * 0.67) {

//錄製完成

            [selfrecordComplete];

        }

    }

    [selfstopAnimation];

self.changeBtn.hidden = self.flashModelBtn.hidden = NO;

}

- (BOOL)isInBtnRect:(CGPoint)point

{

CGFloat x = point.x;

CGFloat y = point.y;

return  (x>self.tapBtn.left && x<=self.tapBtn.right) && (y > (self.tapBtn.top  + self.bottomView.y) && y <= (self.tapBtn.bottom  + self.bottomView.y));

}

//po self.tapBtn.left 130  self.tapBtn.right 190  xCGFloat146  yCGFloat523

//self.tapBtn.top 12 self.bottomView.bottom 568

- (void)isFitCondition:(BOOL)condition

{

if (condition) {

self.cancelTip.text = @"↑上滑取消";

self.cancelTip.backgroundColor = [UIColorclearColor];

self.cancelTip.textColor = BLUECOLOR;

self.progressView.backgroundColor = BLUECOLOR;

    }else{

self.progressView.backgroundColor = REDCOLOR;

self.cancelTip.text = @"鬆手取消";

self.cancelTip.backgroundColor = REDCOLOR;

self.cancelTip.textColor = [UIColorwhiteColor];

    }

}

- (void)startAnimation

{

NSLog(@"startAnimation");

if (self.status == VideoStatusEnded) {

self.status = VideoStatusStarted;

        [UIViewanimateWithDuration:0.5animations:^{

self.cancelTip.alpha = self.progressView.alpha = 1.0;

self.tapBtn.alpha = 0.0;

self.tapBtn.transform = CGAffineTransformMakeScale(2.0, 2.0);

        } completion:^(BOOL finished) {

            [selfstopLink];

            [self.linkaddToRunLoop:[NSRunLoopmainRunLoop] forMode:NSRunLoopCommonModes];

        }];

    }

}

- (void)stopAnimation{

NSLog(@"stopAnimation");

if (self.status == VideoStatusStarted) {

self.status = VideoStatusEnded;

        [selfstopLink];

        [selfstopRecord];

        [UIViewanimateWithDuration:0.5animations:^{

self.cancelTip.alpha = self.progressView.alpha = 0.0;

self.tapBtn.alpha = 1.0;

self.tapBtn.transform = CGAffineTransformMakeScale(1.0, 1.0);

        } completion:^(BOOL finished) {

self.progressWidth.constant = SCREEN_WIDTH;

        }];

    }

}

- (CADisplayLink *)link{

if (! _link) {

_link = [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(refresh:)];

self.progressWidth.constant = SCREEN_WIDTH;

        [selfstartRecord];

    }

return_link;

}

- (void)stopLink

{

_link.paused = YES;

    [_linkinvalidate];

_link = nil