1.1--> 导入需要的文件类库
#import<AVFoundation/AVFoundation.h>
#import<MobileCoreServices/MobileCoreServices.h>
#import<AssetsLibrary/AssetsLibrary.h>
1.2-->签约代理
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
2-->检测应用是否开启读取相册的权限
// 申请打开视频库权限
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted
|| authStatus == AVAuthorizationStatusDenied) {
NSLog(@"摄像头已被禁用,您可在设置应用程序中进行开启");
return;
}
3-->打开视频库
// 打开视频库
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.mediaTypes = @[(NSString *)kUTTypeMovie];
[self presentViewController:picker animated:YES completion:NULL];
} else {
NSLog(@"手机不支持摄像");
}
4-->选取视频后
//视频选择完成后调用- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
//获取选择的媒体类型
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
// 获取相册内容
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld_compressedVideo.mp4",time(NULL)]];
// 保存路径
NSLog(@"compressedVideoSavePath : %@",path);
//压缩
AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
//导出品质
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {
// 压缩分辨率
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPreset1280x720];
//输出URL
exportSession.outputURL = [NSURL fileURLWithPath:path];
//优化网络
exportSession.shouldOptimizeForNetworkUse = true;
//转换后的格式
exportSession.outputFileType = AVFileTypeMPEG4;
//异步导出
[exportSession exportAsynchronouslyWithCompletionHandler:^{
// 如果导出的状态为完成
if ([exportSession status] == AVAssetExportSessionStatusCompleted) {
// 保存路径
[self saveVideo:[NSURL fileURLWithPath:path]];
NSLog(@"压缩完毕,压缩后大小 %f MB",[self fileSize:[NSURL fileURLWithPath:path]]);
}else{
NSLog(@"当前压缩进度:%f",exportSession.progress);
}
NSLog(@"%@",exportSession.error);
}];
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
//存入相册
- (void)saveVideo:(NSURL *)outputFileURL
{
//ALAssetsLibrary提供了我们对iOS设备中的相片、视频的访问。
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
//写入相册
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"保存视频失败:%@",error);
} else {
NSLog(@"保存视频到相册成功");
}
}];
}
//获取压缩后的大小
- (CGFloat)fileSize:(NSURL *)path
{
//将数据大小格式转换为*Mb
return [[NSData dataWithContentsOfURL:path] length]/1024.00 /1024.00;
}