iOS 选择照片、视频、拍照并压缩视频获取压缩保存后路径的方法

iOS10 相机相册等权限的使用、检测并引导用户开启权限  <- 点击这里

#import "ViewController.h"

#import

#import

#import


@interfaceViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic ,strong)NSString * path;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

//    [self openVideoLibrary];

    [selfopenPhotoLibrary];

//    [self openCamera];


}

#pragma mark ————————— 打开视频库 —————————————

- (void)openVideoLibrary{

    //打开控制器

    UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

    //设置媒体类型

    picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//打开相册库,用户与电脑同步的,不能删除

    

    picker.delegate =self;


    picker.allowsEditing =NO;

    NSString *requiredMediaType1 = (NSString *)kUTTypeMovie;

    NSArray *arrMediaTypes = [NSArrayarrayWithObjects:requiredMediaType1,nil];

    [picker setMediaTypes: arrMediaTypes];

    

    //显示控制器

    [selfpresentViewController:pickeranimated:YEScompletion:nil];

    

}


#pragma mark ————————— 打开图片库 —————————————

- (void)openPhotoLibrary {

    //打开控制器

    UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

    //    设置媒体类型

    picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//打开相册,获取资源,视频和图片用户拍摄的照片可以删除,

    picker.delegate =self;

    //显示控制器

    [selfpresentViewController:pickeranimated:YEScompletion:nil];

}


#pragma mark ————————— 打开摄像头拍照 —————————————

- (void)openCamera {

    //打开控制器

    UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

    //    设置媒体类型

    picker.sourceType=UIImagePickerControllerSourceTypeCamera;//设置摄像头类型摄像头

    picker.delegate =self;

    //显示控制器

    [selfpresentViewController:pickeranimated:YEScompletion:nil];

}



#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if([mediaTypeisEqualToString:@"public.movie"]) {

        

        //如果是视频返回的是URL

        NSURL * url=info[UIImagePickerControllerMediaURL];

        NSLog(@"视频地址 = %@",url);

        //使用媒体工具压缩

        [selfcompressVideo:url];

    }else{

        

        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

        NSLog(@"选择的照片 = %@",image);

        

    }

    //关闭控制器

    [selfdismissViewControllerAnimated:YEScompletion:nil];

    

}


#pragma mark ————————— 压缩视频 —————————————

- (void)compressVideo:(NSURL*)url {

    NSLog(@"压缩");

    //使用媒体工具(AVFoundation框架下的)

    

    //Asset 资源可以是图片音频视频

    AVAsset *asset=[AVAssetassetWithURL:url];

    

   //设置压缩的格式

    AVAssetExportSession *session=[AVAssetExportSessionexportSessionWithAsset:assetpresetName:AVAssetExportPresetMediumQuality];//mediumquality:中等质量

    

    //设置导出路径

    NSString *path=[NSTemporaryDirectory()stringByAppendingPathComponent:[selfgetVideoNameBaseCurrentTime]];

    

   //创建文件管理类导出失败,删除已经导出的

    NSFileManager *manager=[[NSFileManageralloc]init];

    

   //删除已经存在的

    [manager removeItemAtPath:patherror:NULL];

    

    //设置到处路径

    session.outputURL=[NSURLfileURLWithPath:path];

    

   //设置输出文件的类型

    session.outputFileType=AVFileTypeQuickTimeMovie;

    

   //开辟子线程处理耗时操作

    [sessionexportAsynchronouslyWithCompletionHandler:^{

        

        NSLog(@"导出完成!路径:%@",path);

//        _path = path;

       

    }];

}


#pragma mark ————————— 以当前时间合成视频名称 —————————————

- (NSString *)getVideoNameBaseCurrentTime {

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];

    return [[dateFormatterstringFromDate:[NSDatedate]] stringByAppendingString:@".mov"];

}


#pragma mark ————————— 删除文件 —————————————

- (BOOL)deleteDir:(NSString*)dirPath {

   //如果存在临时文件的配置文件

    if([[NSFileManagerdefaultManager] fileExistsAtPath:dirPath]) {

        NSError *error=nil;

        return [[NSFileManagerdefaultManagerremoveItemAtPath:dirPath error:&error];

    }

    return  NO;

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end



你可能感兴趣的:(iOS 选择照片、视频、拍照并压缩视频获取压缩保存后路径的方法)