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

相关文章

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

java常见报错及解决方案总结

《java常见报错及解决方案总结》:本文主要介绍Java编程中常见错误类型及示例,包括语法错误、空指针异常、数组下标越界、类型转换异常、文件未找到异常、除以零异常、非法线程操作异常、方法未定义异常... 目录1. 语法错误 (Syntax Errors)示例 1:解决方案:2. 空指针异常 (NullPoi