1.1--> 導(dǎo)入需要的文件類(lèi)庫(kù)
#import<AVFoundation/AVFoundation.h>
#import<MobileCoreServices/MobileCoreServices.h>
#import<AssetsLibrary/AssetsLibrary.h>
1.2-->簽約代理
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
2-->檢測(cè)應(yīng)用是否開(kāi)啟讀取相冊(cè)的權(quán)限
//? ? 申請(qǐng)打開(kāi)視頻庫(kù)權(quán)限
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted
|| authStatus == AVAuthorizationStatusDenied) {
NSLog(@"攝像頭已被禁用,您可在設(shè)置應(yīng)用程序中進(jìn)行開(kāi)啟");
return;
}
3-->打開(kāi)視頻庫(kù)
//? ? 打開(kāi)視頻庫(kù)
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(@"手機(jī)不支持?jǐn)z像");
}
4-->選取視頻后
//視頻選擇完成后調(diào)用- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
//獲取選擇的媒體類(lèi)型
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
//? ? ? ? 獲取相冊(cè)內(nèi)容
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];
//導(dǎo)出品質(zhì)
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {
//? ? ? ? ? ? 壓縮分辨率
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPreset1280x720];
//輸出URL
exportSession.outputURL = [NSURL fileURLWithPath:path];
//優(yōu)化網(wǎng)絡(luò)
exportSession.shouldOptimizeForNetworkUse = true;
//轉(zhuǎn)換后的格式
exportSession.outputFileType = AVFileTypeMPEG4;
//異步導(dǎo)出
[exportSession exportAsynchronouslyWithCompletionHandler:^{
// 如果導(dǎo)出的狀態(tài)為完成
if ([exportSession status] == AVAssetExportSessionStatusCompleted) {
//? ? ? ? ? ? ? ? ? ? 保存路徑
[self saveVideo:[NSURL fileURLWithPath:path]];
NSLog(@"壓縮完畢,壓縮后大小 %f MB",[self fileSize:[NSURL fileURLWithPath:path]]);
}else{
NSLog(@"當(dāng)前壓縮進(jìn)度:%f",exportSession.progress);
}
NSLog(@"%@",exportSession.error);
}];
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
//存入相冊(cè)
- (void)saveVideo:(NSURL *)outputFileURL
{
//ALAssetsLibrary提供了我們對(duì)iOS設(shè)備中的相片、視頻的訪(fǎng)問(wèn)。
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
//寫(xiě)入相冊(cè)
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"保存視頻失敗:%@",error);
} else {
NSLog(@"保存視頻到相冊(cè)成功");
}
}];
}
//獲取壓縮后的大小
- (CGFloat)fileSize:(NSURL *)path
{
//將數(shù)據(jù)大小格式轉(zhuǎn)換為*Mb
return [[NSData dataWithContentsOfURL:path] length]/1024.00 /1024.00;
}