ios实现拍摄视频与显示在界面上

devtools/2024/9/23 14:02:02/

1、添加录音和拍摄权限

NSMicrophoneUsageDescription

Privacy - Camera Usage Description

2、代码


#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MobileCoreServices/MobileCoreServices.h>// 接下来是你的 ViewController 的实现代码,不需要修改。@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 创建显示视频的视图self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];[self.view addSubview:self.imageView];// 创建拍摄视频按钮UIButton *recordVideoButton = [UIButton buttonWithType:UIButtonTypeSystem];[recordVideoButton setTitle:@"拍摄视频" forState:UIControlStateNormal];recordVideoButton.frame = CGRectMake(50, 350, 100, 50);[recordVideoButton addTarget:self action:@selector(recordVideo) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:recordVideoButton];
}- (void)recordVideo {// 检查相机是否可用if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:@"相机不可用" preferredStyle:UIAlertControllerStyleAlert];[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];[self presentViewController:alert animated:YES completion:nil];return;}// 创建 UIImagePickerControllerUIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;imagePicker.mediaTypes = @[(NSString *)kUTTypeMovie];imagePicker.delegate = self;[self presentViewController:imagePicker animated:YES completion:nil];
}// 拍摄视频完成后调用的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {NSString *mediaType = info[UIImagePickerControllerMediaType];if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {self.videoURL = info[UIImagePickerControllerMediaURL];[self displayVideo];}[picker dismissViewControllerAnimated:YES completion:nil];
}// 用户取消拍摄视频时调用的代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {[picker dismissViewControllerAnimated:YES completion:nil];
}// 显示拍摄的视频
- (void)displayVideo {AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:self.videoURL];self.player = [AVPlayer playerWithPlayerItem:playerItem];self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];self.playerLayer.frame = CGRectMake(50, 100, 200, 200);[self.view.layer addSublayer:self.playerLayer];[self.player play];
}@end


http://www.ppmy.cn/devtools/39506.html

相关文章

部署达梦数据库主从配置详细操作DM8

服务器配置 主库 192.168.81.128 实例名 dm-1 从库 192.168.81.129 实例名 dm-2 以下安装部署主从服务器都操作 关闭防火墙 systemctl stop firewalld && systemctl disable firewalld 注意安装前必须创建 dmdba 用户&#xff0c;禁止使用 root 用户安装数据库。…

【QA】Java集合常用的函数

文章目录 前言Collection接口通用函数 | Collections工具类通用函数 | List接口 Set接口List接口ArrayListLinkedList Set接口TreeSetHashSetLinkedHashSet Map接口通用函数TreeMapHashMapLinkedHashMap 前言 本文介绍Java集合中常用的函数。 Collection接口 通用函数 | Co…

普通组件的注册-局部注册和全局注册

目录 一、局部注册和全局注册-概述 二、局部注册的使用示例 三、全局注册的使用示例 一、局部注册和全局注册-概述 组件注册有两种方式&#xff1a; 局部注册&#xff1a;只能在注册的组件内使用。使用方法&#xff1a;创建.vue文件&#xff0c;在使用的组件内导入并注册。…

Linux常见指令2️⃣

目录 cp指令&#xff08;重要&#xff09; mv指令&#xff08;重要&#xff09; cat、tac head、tail指令&#xff08;重要&#xff09; 知识点 时间相关的指令 知识点&#xff1a; Cal指令 grep 指令 zip/unzip指令 知识点 cp指令&#xff08;重要&#xff09; 语法…

使用Express+Node.js搭建网站

Express是一个基于Node.js平台的快速、开放、极简的Web开发框架。它的作用是专门用来创建Web服务器&#xff0c;与Node.js内置的http模块功能相似&#xff0c;但更为简便和高效。 Express中文官网&#xff1a;Express - 基于 Node.js 平台的 web 应用开发框架 - Express中文文…

MySQL数据库及数据表的创建

1.创建一个名叫 db_classes 的数据库&#xff1a; 创建一个叫 db_classes 的数据库MySQL命令&#xff1a; create database db_classes; 运行效果&#xff1a; 创建数据库后查看该数据库基本信息MySQL命令&#xff1a; show create database db_classes; 运行效果&#xff…

【半夜学习MySQL】库的操作(含库的创建、删除、修改、备份操作/查看mysql连接情况/字符集和校验规则详谈)

&#x1f3e0;关于专栏&#xff1a;半夜学习MySQL专栏用于记录MySQL数据相关内容。 &#x1f3af;每天努力一点点&#xff0c;技术变化看得见 文章目录 创建数据库字符集和校验规则查看字符集合校验规则校验规则对数据库的影响 操纵数据库数据备份和恢复查看连接情况 创建数据库…

【Vue】Vue的核心

目录 计算属性-computed插值语法实现methods实现计算属性实现使用使用总结&#xff1a; 监视属性-watch监视的两种写法&#xff1a;深度监视备注&#xff1a; computed和watch之间的区别 绑定样式class样式绑定字符串写法数组写法对象写法 style样式绑定对象式1对象式2数组式 条…