ios开发之AVFoundation总结

2024-05-08 13:48
文章标签 总结 开发 ios avfoundation

本文主要是介绍ios开发之AVFoundation总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1, Using Assets [自己理解为:数据的来源] 
这个资源可以来自自己的ipod媒体库或图片也可以时文件 
creating an Asset Object 
NSRUL *url = 后跟一个网址 如电影文件等资源 
AVURLAsset *ansset = [[AVURLSset alloc] initwithURL:url  options:nil]; 
2.获得一个视频的图像 
使用AVASsetImageGenerator类来实现 
用来生成图像序列 
3.Playback 
我们在播放视频时可以使用AVPlayer和AVQueuePlayer播放AVPlayer是AVQueuePlayer的父类 
a先创建一个路径 
b可以使用AVPlayerItem加载路径 
c使用AVPlayer播放文件 
当然我们还可以控制它的播放速度 
使用rate属性它是一个介于0.0--1.0之间的数 
 
我们也可以播放多个项目 
NSArray *items  = // 设置一个播放的组合 
AVQueuePlayer *queueplayer = [[AVQueuePlayer alloc]initwithItems:items]; 
然后使用AVPlayerItem  
AVPlayerItem *anItem = // get  a player item 
使用canInsertItem:afterItem 测试 
4.Media capture 
我们可以配置预设图片的质量和分辨率 
Symbol                                                    Resolution        Comments 
AVCaptureSessionPresetHigh        High                  Highest recording quality. This varies per device. 
AVCaptureSessionPresetMedium      Medium            Suitable for WiFi sharing. The actual values may change. 
AVCaptureSessionPresetLow        Low                        Suitable for 3g sharing. The actual values may change. 
AVCaptureSessionPreset640x480    640x480      VGA 
AVCaptureSessionPreset1280x720    1280x720    720p HD 
AVCaptureSessionPresetPhoto                Photo      Full photo resolution. This is not supported for video output 
判断一个设备是否适用 
AVCaptreSessuion *session = [[AVCaptureSession alloc]init]; 
if([session canSetSessionPreset:AVCaptureSessionPrese 1280x720]){ 
session.sessionPreset = AVCaptureSessionPreset 1280x720; 
}else{ 
// Handle the failure. 

当然在 
[session beginConfigration], 
[session commitconfiguration]之间配置重新添加你想要适用的设备以及删除以前的设备等操作 
5.当我们不知道设备的一些特性时我们可以使用以下代码查找相应的设备 
NSArray *devices = [AVCaptureDevice devices]; 
fo(AVCaptureDevice *device in device){ 
NSLogO("Device name %@",[devic localizedName]); 
当然还可以判断设备的位置 
if([device hasMediaType:AVMediaTypeVideo]){ 
if([device postion] == AVCaptureDevicePostionBack){ 
nslog(@"Device postion :back"); 
}else{ 
NSLog(@"Device postion :front"); 



下面的demo说明如何找到视频输入设备 
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 
NsMutableArray *torchDevices =  [[NSMutableArray alloc]init];  
for(AVCaptureDevice *device in devices){ 
if([device hasTorch]&&[device supportsAVCaptureSessionPreset:AVCaptureSessionPreset 640x480]){ 
[torchDevices addObject:device]; 


 
6设备间切换 
AVCaptureSession *session = //一个设备session 
[session beginConfiguration]; 
 
[session removeInput:frontFacingCameraDeviceInput]; 
[session AddInput:backFacikngCameraDeviceInput]; 
 
[session commitConfiguration]; 
 
7 配置AVCaptureDeviceInput  
AVCaptureSession *captureSession = <#Get a capture session#>;  
AVCaptureDeviceInput *captureDeviceInput = <#Get a capture device input#>;  
// 检查是否适用 
if ([captureSession canAddInput:captureDeviceInput]) { 
// 适用则添加 
[captureSession addInput:captureDeviceInput]; 
} else { 
// Handle the failure. 

8 配置AVCaptureOutput 
输出的类型: 
  a.AVCaptureMovieFileOutput 输出一个电影文件 
b.AVCaptureVideoDataOutput 输出处理视频帧被捕获 
c.AVCaptureAudioDataOutput 输出音频数据被捕获 
d.AVCaptureStillImageOutput 捕获元数据 
AVCaptureSession *captureSession = <#Get a capture session#>; 
AVCaptureMovieFileOutput *movieInput = <#Create and configure a movie output#>;  
if ([captureSession canAddOutput:movieInput]) { 
[captureSession addOutput:movieInput]; 
} else { 
// Handle the failure. 

9 保存到一个电影文件 
AVCaptureMovieFileOutput *aMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 
CMTime maxDuration = <#Create a CMTime to represent the maximum duration#>; aMovieFileOutput.maxRecordedDuration = maxDuration;  
aMovieFileOutput.minFreeDiskSpaceLimit = <#An appropriate minimum given the quality of the movie format and the duration#>; 
10 录音设备 
The delegate must conform to the  
AVCaptureFileOutputRecordingDelegate  
protocol,  
and must implement the  
captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error: method. 
11 像素和编码格式 
iphone 3G      iphone 3GS                      iphone 4 
yuvs,2vuy,BGRA,jpeg    420f,420v,BGRA,jpeg        420f, 420v, BGRA, jpeg 
12  静态图像捕捉 
AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil]; [stillImageOutput setOutputSettings:outputSettings]; 
13 重力模式 
The preview layer supports three gravity modes that you set using videoGravity: 
● AVLayerVideoGravityResizeAspect: This preserves the aspect ratio, leaving black bars where the 
video does not fill the available screen area. 
● AVLayerVideoGravityResizeAspectFill: This preserves the aspect ratio, but fills the available screen area, cropping the video when necessary. 
● AVLayerVideoGravityResize: This simply stretches the video to fill the available screen area, even if doing so distorts the image. 
 
 
设备之间切换 
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position 

    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 
    for ( AVCaptureDevice *device in devices ) 
        if ( device.position == position ) 
            return device; 
    return nil; 

 
- (void)swapFrontAndBackCameras { 
    // Assume the session is already running 
 
    NSArray *inputs = self.session.inputs; 
    for ( AVCaptureDeviceInput *input in inputs ) { 
        AVCaptureDevice *device = input.device; 
        if ( [device hasMediaType:AVMediaTypeVideo] ) { 
            AVCaptureDevicePosition position = device.position; 
            AVCaptureDevice *newCamera = nil; 
            AVCaptureDeviceInput *newInput = nil; 
 
            if (position == AVCaptureDevicePositionFront) 
                newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack]; 
            else 
                newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront]; 
            newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil]; 
 
            // beginConfiguration ensures that pending changes are not applied immediately 
            [self.session beginConfiguration]; 
 
            [self.session removeInput:input]; 
            [self.session addInput:newInput]; 
 
            // Changes take effect once the outermost commitConfiguration is invoked. 
            [self.session commitConfiguration]; 
            break; 
        } 
    }  

这篇关于ios开发之AVFoundation总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/970496

相关文章

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Nginx Location映射规则总结归纳与最佳实践

《NginxLocation映射规则总结归纳与最佳实践》Nginx的location指令是配置请求路由的核心机制,其匹配规则直接影响请求的处理流程,下面给大家介绍NginxLocation映射规则... 目录一、Location匹配规则与优先级1. 匹配模式2. 优先级顺序3. 匹配示例二、Proxy_pa