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开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

Git提交代码详细流程及问题总结

《Git提交代码详细流程及问题总结》:本文主要介绍Git的三大分区,分别是工作区、暂存区和版本库,并详细描述了提交、推送、拉取代码和合并分支的流程,文中通过代码介绍的非常详解,需要的朋友可以参考下... 目录1.git 三大分区2.Git提交、推送、拉取代码、合并分支详细流程3.问题总结4.git push

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.