CNLiveViewController.m 2.87 KB
//
//  CNLiveViewController.m
//  CNLiveAVCamera
//
//  Created by jyyjkt on 05/05/2019.
//  Copyright (c) 2019 jyyjkt. All rights reserved.
//

#import "CNLiveViewController.h"
#import "XFCameraController.h"

@interface CNLiveViewController ()

@end

@implementation CNLiveViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    UIButton *takeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    takeBtn.frame = CGRectMake(0, 0, 150, 150);
    [takeBtn setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:takeBtn];
    [takeBtn addTarget:self action:@selector(takeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)takeToXFPhoto
{
    XFCameraController *cameraController = [[XFCameraController alloc] init];
    cameraController.isCanShootVideo = YES;
    NSString *tempName = [NSString stringWithFormat:@"%@/",@"name"];
    
    NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempName];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = NO;
    
    // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
    BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    
    if ( !(isDir == YES && existed == YES) ) {
        
        // 在 temp 目录下创建一临时文件
        [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    cameraController.outPutPath = @"";
    
    __weak XFCameraController *weakCameraController = cameraController;
    __weak typeof(self) weakSelf = self;
    //拍照完成
    cameraController.takePhotosCompletionBlock = ^(UIImage *image, NSError *error) {
        NSLog(@"takePhotosCompletionBlock");
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakCameraController dismissViewControllerAnimated:YES completion:nil];
        });
    };
    
    //录制完成
    cameraController.zxshootCompletionBlock = ^(PHAsset *asset, NSURL *videoUrl, CGFloat videoTimeLength, UIImage *thumbnailImage, NSError *error) {
        NSLog(@"shootCompletionBlock");
        if (!error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakCameraController dismissViewControllerAnimated:YES completion:nil];
            });
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"录制失败");
            });
        }
        
    };
    
    [self presentViewController:cameraController animated:YES completion:nil];
}

- (void)takeBtnAction:(UIButton *)sender {
    
    
    [self takeToXFPhoto];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end