本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正。
本文相关目录:
================== 所属文集:6.0 图形和多媒体 ==================
6.1 Quartz 2D->1.1 Quartz 2D 简介
6.2 核心动画->1.0 CALayer的简介
··················· 1.1 CALayer的基本属性
··················· 1.2 CALayer的创建 & 非根layer的隐式动画
··················· 2.0 Core Animation(核心动画)
··················· 3.0 核心动画 & UIView动画
··················· 4.0 常用动画效果
6.3 音频->1.0 录音 未发布
············· 2.0 音效/音乐 未发布
············· iOS项目Demo - 仿·QQ音乐播放器 未发布
6.4 视频->1.0 视频播放方案
············· 2.0 视频特效:画中画
············· 3.0 视频功能
············· iOS项目Demo - 仿·慕课网视频播放器 未发布
6.5 直播 未发布
6.5 直播-> iOS项目Demo - 仿·斗鱼直播 未发布
================== 所属文集:6.0 图形和多媒体 ==================
视频目录

本文目录

功能1:视频截图

代码7:视频截图 demo
编译环境:Xcode 8.0
模拟器版本:iOS 10
Swift版本:3.0
【OC语言】
// 为了简化代码,只做截图功能,不附加播放器功能
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - 视频截图
- (IBAction)star:(UIButton *)sender {
// ① 本地媒体文件
NSString *path = [[NSBundle mainBundle]pathForResource:@"1.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
// 创建AVAsset对象,指定素材
AVAsset *asset = [AVAsset assetWithURL:url];
// ② 通过远程URL, 创建控制器 MPMoviePlayerViewController
// NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/3e35cbb0-c8e5-4827-9614-b5a355259010/L.mp4"];
// AVAsset *asset = [AVAsset assetWithURL:remoteURL];
// 1.创建截图对象
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc]initWithAsset:asset];
// 2.开始截图(参数1:Times表示要截取的视频的时间,必须是对象,如果用在播放器中,则此处应是当前视频播放的时间)
[gen generateCGImagesAsynchronouslyForTimes:@[@(20.0)]
completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
// 把截图显示在imageView上的操作放在主线程(更新UI)
dispatch_sync(dispatch_get_main_queue(), ^{
// 类型转换
self.imageView.image = [UIImage imageWithCGImage:image];
});
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
【Swift 语言】(暂无,以后补上)
功能2:视频压缩导出

备注:
1、真机运行后,如何找到压缩的视频?
首先,打开 Xcode -> Window -> Devices 后如下图中的方法操作

2、iOS 10以后相册权限配置
Key:Privacy - Photo Library Usage Description
Value:photoLibraryDesciption
代码8:视频压缩导出 demo
编译环境:Xcode 8.0
模拟器版本:iOS 10
Swift版本:3.0
【OC语言】
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
// 1.获取手机上的视频
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 1.1 获取手机相册
UIImagePickerController *vc = [[UIImagePickerController alloc]init];
// 设置来源
vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 设置类型
vc.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// 1.2 设置代理
vc.delegate = self;
// 1.3 展示界面
[self presentViewController:vc animated:YES completion:nil];
}
// 1.4 实现 UIImagePickerControllerDelegate 代理方法
// 获取所点击的图片or视频 (实现代理方法之后,界面不会退出)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%@",info);
// 2.进行导出 压缩
// 2.1 创建导出对象(通过 UIImagePickerControllerMediaURL 获取到点击的视频)
NSURL *url = info[UIImagePickerControllerMediaURL];
// 指定素材
AVAsset *asset = [AVAsset assetWithURL:url];
// 设置导出的类型
AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:asset
presetName:AVAssetExportPresetLowQuality];
// 2.2 设置导出存放的地方
// 获取沙盒路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
// 设置保存的视频的路径(包括视频名)
path = [path stringByAppendingPathComponent:@"123.mp4"];
// 设置导出位置
session.outputURL = [NSURL fileURLWithPath:path];
// 设置导出的视频类型
session.outputFileType = AVFileTypeQuickTimeMovie;
//2.3 导出,开始压缩
[session exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"导出完成");
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
【Swift 语言】(暂无,以后补上)
功能3:视频录制

备注:
1、真机运行后,如何找到录制的视频?
首先,打开 Xcode -> Window -> Devices 后,方法操作和视频压缩类似
2、iOS 10以后麦克风权限配置
Key:Privacy - Microphone Usage Description
Value:microphoneDesciption
3、iOS 10以后摄像头权限配置
Key:Privacy - Camera Usage Description
Value:cameraDesciption
代码9:视频录制 demo
编译环境:Xcode 8.0
模拟器版本:iOS 10
Swift版本:3.0
【OC语言】
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVCaptureFileOutputRecordingDelegate>
@property (nonatomic,strong)AVCaptureMovieFileOutput *output;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - 开始录制视频
- (IBAction)start:(UIButton *)sender {
// 1.创建输入对象
// 获取麦克风
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioInput = [[AVCaptureDeviceInput alloc]initWithDevice:audioDevice error:nil];
// 获取摄像头
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc]initWithDevice:videoDevice error:nil];
// 2.创建输出对象
AVCaptureMovieFileOutput *output = [[AVCaptureMovieFileOutput alloc]init];
self.output = output;
// 3.创建会话连接输入和输出
AVCaptureSession *session = [[AVCaptureSession alloc]init];
// 判断是否可以添加到会话
if ([session canAddInput:videoInput]) {
[session addInput:videoInput];
}
if ([session canAddInput:audioInput]) {
[session addInput:audioInput];
}
if ([session canAddOutput:output]) {
[session addOutput:output];
}
// 4.添加到图层
AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];
layer.frame = CGRectMake(15,30,300, [UIScreen mainScreen].bounds.size.height-80);
[self.view.layer addSublayer:layer];
// 5.开始会话
[session startRunning];
// 6.设置视频的存放位置
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
path = [path stringByAppendingPathComponent:@"123.mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
// 7.开始录制
[output startRecordingToOutputFileURL:url recordingDelegate:self];
}
#pragma mark - 停止录制视频
- (IBAction)stop:(UIButton *)sender {
[self.output stopRecording];
}
#pragma mark - 代理方法:开始录制视频
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections{
NSLog(@"开始录制视频");
}
#pragma mark - 代理方法:停止录制视频
-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
NSLog(@"停止录制视频");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
本文源码 Demo 详见 Github(方便的话,帮忙点个Star,谢啦!)
https://github.com/shorfng/iOS_6.0_Graphics_and_multimedia.git
作者:蓝田(Loto)
【作品发布平台】
① 简书
② 博客园
③ Gitbook(如果觉得文章太长,请阅读此平台发布的文章)
【代码托管平台】
【如有疑问,请通过以下方式交流】
① 评论区回复
② 发送邮件
至 shorfng@126.com
本文版权归作者和本网站共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,谢谢合作。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
-
支付宝扫一扫 向我打赏
-
你也可以微信 向我打赏
网友评论