ClipViewController.m 7.66 KB
//
//  ClipViewController.m
//  Camera
//
//  Created by wzh on 2017/6/6.
//  Copyright © 2017年 wzh. All rights reserved.
//

#import "ClipViewController.h"
#import "TKImageView.h"
#import "ClipConfirmView.h"
#define SelfWidth [UIScreen mainScreen].bounds.size.width
#define SelfHeight  [UIScreen mainScreen].bounds.size.height
static const CGFloat kToolBarHeight = 120.0f;
@interface ClipViewController ()

@property (nonatomic, assign) BOOL isClip;

@property (nonatomic, strong) TKImageView *tkImageView;

// 预览页面
@property (nonatomic, strong) ClipConfirmView *clipConfirmView;

// 底部工具栏
@property (nonatomic, strong) UIView *editToolBarView;

@end

@implementation ClipViewController{
    // 确认按钮
    UIButton *sureBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor blackColor]];
    [self.mainLayout setBackgroundColor:[UIColor blackColor]];
    [self createdTkImageView];
    
    [self createdTool];
    
    [self createdClipConfirmView];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [_editToolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.left.right.mas_equalTo(self.mainLayout);
//        make.height.mas_equalTo(@(kToolBarHeight));
//    }];
//    [_tkImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(self.mainLayout);
//        make.bottom.mas_equalTo(self->_editToolBarView.mas_top);
//        make.height.mas_equalTo(@(SelfHeight - kToolBarHeight));
//    }];
//    [_clipConfirmView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(self->_tkImageView);
//    }];
}

- (void)createdTkImageView
{
    _tkImageView = [[TKImageView alloc] initWithFrame:CGRectMake(0, 0, SelfWidth, SelfHeight - kToolBarHeight)];
//    _tkImageView = [[TKImageView alloc] initWithFrame:CGRectMake(0, 0, SelfWidth, SelfHeight)];
    [self.mainLayout addSubview:_tkImageView];
    //需要进行裁剪的图片对象
    _tkImageView.toCropImage = _image;
    //是否显示中间线
    _tkImageView.showMidLines = YES;
    //是否需要支持缩放裁剪
    _tkImageView.needScaleCrop = NO;
    //是否显示九宫格交叉线
    _tkImageView.showCrossLines = YES;
    _tkImageView.cornerBorderInImage = NO;
    _tkImageView.cropAreaCornerWidth = 44;
    _tkImageView.cropAreaCornerHeight = 44;
    _tkImageView.minSpace = 30;
    _tkImageView.cropAreaCornerLineColor = [UIColor whiteColor];
    _tkImageView.cropAreaBorderLineColor = [UIColor whiteColor];
    _tkImageView.cropAreaCornerLineWidth = 6;
    _tkImageView.cropAreaBorderLineWidth = 1;
    _tkImageView.cropAreaMidLineWidth = 20;
    _tkImageView.cropAreaMidLineHeight = 6;
    _tkImageView.cropAreaMidLineColor = [UIColor whiteColor];
    _tkImageView.cropAreaCrossLineColor = [UIColor whiteColor];
    _tkImageView.cropAreaCrossLineWidth = 0.5;
    _tkImageView.initialScaleFactor = .8f;
    _tkImageView.cropAspectRatio = self.cropAspectRatio;
    _tkImageView.maskColor = [UIColor clearColor];
    
    self.isClip = NO;
}

- (void)createdTool
{
    _editToolBarView = [[UIView alloc] initWithFrame:CGRectMake(0, SelfHeight - kToolBarHeight, SelfWidth, kToolBarHeight)];
    _editToolBarView.backgroundColor = [UIColor blackColor];
    _editToolBarView.alpha = 0.8;
    [self.mainLayout addSubview:_editToolBarView];
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(((SelfWidth / 3.0) - 50)/2.0, (kToolBarHeight - 50)/2.0, 50, 50);
    [cancleBtn setImage:[UIImage imageNamed:@"canclePhoto"] forState:UIControlStateNormal];
    
    [cancleBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [_editToolBarView addSubview:cancleBtn];
    
    UIButton *clipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    clipBtn.frame = CGRectMake(((SelfWidth) - 50)/2.0, (kToolBarHeight - 50)/2.0, 50, 50);
    [clipBtn setImage:[UIImage imageNamed:@"clipPhoto"] forState:UIControlStateNormal];
    [clipBtn setImage:[UIImage imageNamed:@"backPhoto"] forState:UIControlStateSelected];
    [clipBtn addTarget:self action:@selector(clip:) forControlEvents:UIControlEventTouchUpInside];
    [_editToolBarView addSubview:clipBtn];
    
    sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(((SelfWidth/3.0) - 50)/2.0 + (SelfWidth * 2.0/3.0), (kToolBarHeight - 50)/2.0, 50, 50);
    [sureBtn setImage:[UIImage imageNamed:@"surePhoto"] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    
    [_editToolBarView addSubview:sureBtn];
    if(_cropAspectRatio > 0) {
        sureBtn.hidden = YES;
    }
}

- (void)createdClipConfirmView{
    _clipConfirmView = [[ClipConfirmView alloc] initWithFrame:CGRectMake(0, 0, SelfWidth, SelfHeight - kToolBarHeight)];
    [self.mainLayout addSubview:_clipConfirmView];
    _clipConfirmView.hidden = YES;
}

- (void)back{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)clip:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    
    self.isClip = btn.selected;
    
    if (btn.selected == YES) {
        [_clipConfirmView setImage:[_tkImageView currentCroppedImage]];
        _clipConfirmView.hidden = NO;
        sureBtn.hidden = NO;
        //_tkImageView.toCropImage = [_tkImageView currentCroppedImage];
    }else{
        _tkImageView.toCropImage = _image;
        _clipConfirmView.hidden = YES;
        if(_cropAspectRatio > 0) {
            sureBtn.hidden = YES;
        }
    }
}

- (void)sure{
    
    //裁剪
    if (self.isClip == YES) {
        
        //UIImage *image = [_tkImageView currentCroppedImage];
        UIImage *image = [_clipConfirmView getClipImage];
        
        if (self.isTakePhoto) {
            //将图片存储到相册
            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(clipPhoto:)]) {
            
            [self.delegate clipPhoto:image];
        }
    }else{
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(clipPhoto:)]) {
            
            [self.delegate clipPhoto:self.image];
        }
        
        if (self.isTakePhoto) {
            //将图片存储到相册
            UIImageWriteToSavedPhotosAlbum(self.image, self, nil, nil);
        }
    
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.picker dismissViewControllerAnimated:YES completion:nil];
    [self.controller dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
/**
 屏幕方向 >=6.0
 */
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate {
    return NO;
}

/**
 状态条样式 >=7.0
 */
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

/**
 是否隐藏状态条 >=7.0
 */
- (BOOL)prefersStatusBarHidden {
    return YES;
}

/**
 内存预警
 */
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

@end