iOS自定义相机界面,实现微信小视频UI效果

2023-11-21 22:50

本文主要是介绍iOS自定义相机界面,实现微信小视频UI效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在开发的时候,要实现微信小视频的效果只用系统框架中的picker是不行的,它是个navi 而我们需要一个view把它放到tableview后边

所以我们需要自定义一个摄像界面

下边的文章摘抄自http://course.gdou.com/blog/Blog.pzs/archive/2011/12/14/10882.html

看完了相信对你会有很大的帮助



在进行视频捕获时,有输入设备及输出设备,程序通过 AVCaptureSession 的一个实例来协调、组织数据在它们之间的流动。 

程序中至少需要:

● An instance of AVCaptureDevice to represent the input device, such as a camera or microphone

● An instance of a concrete subclass of AVCaptureInput to configure the ports from the input device

● An instance of a concrete subclass of AVCaptureOutput to manage the output to a movie file or still image

● An instance of AVCaptureSession to coordinate the data flow from the input to the output

由上图可以看出,一个AVCaptureSession 可以协调多个输入设备及输出设备。通过 AVCaptureSession 的 addInput、addOutput 方法可将输入、输出设备加入 AVCaptureSession 中。

capture input 及 capture out 之间的联系由 AVCaptureConnection 对象表示,capture input (AVCaptureInput) 有一个或多个输入端口(AVCaptureInputPort 实例) ,capture out(AVCaptureOutput 实例)可接收来自一个或多个输入源的数据。

当一个输入或一个输出被加入到  AVCaptureSession 中时,该 session 会“贪婪地” 在所有兼容的输入端口和输出之间建立连接(AVCaptureConnection),因此,一般不需要手工在输入、输出间建立连接。

输入设备:

AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice: 

                                     [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo] error:nil];

种类有:

AVMediaTypeVideo

AVMediaTypeAudio

输出设备有:

AVCaptureMovieFileOutput 输出到文件

AVCaptureVideoDataOutput 可用于处理被捕获的视频帧

AVCaptureAudioDataOutput 可用于处理被捕获的音频数据

AVCaptureStillImageOutput 可用于捕获带有元数据(MetaData)的静止图像

输出设备对象的创建:

AVCaptureMovieFileOutput  *captureOutput = [[AVCaptureMovieFileOutput allocinit;

一、捕获到视频文件

此时输出设备指定为:AVCaptureMovieFileOutput,其子类 AVCaptureFileOutput 的2个方法用于启动、停止编码输出:

- (void)startRecordingToOutputFileURL:(NSURL *)outputFileURL recordingDelegate:(id < AVCaptureFileOutputRecordingDelegate >)delegate

- (void)stopRecording

程序开始编码输出时,应先启动 AVCaptureSession,再用以上方法启动编码输出。整个步骤:

 创建输入、输出设备、AVCaptureSession对象:

  AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput  deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo]  error:nil];

  AVCaptureDeviceInput *microphone = [AVCaptureDeviceInput  deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeAudio]  error:nil];

  /*We setupt the output*/

  captureOutput = [[AVCaptureMovieFileOutput allocinit]; 

  self.captureSession = [[AVCaptureSession allocinit];

  加入输入、输出设备:

  [self.captureSession addInput:captureInput];

  [self.captureSession addInput:microphone];

  [self.captureSession addOutput:self.captureOutput];

  设置Session 属性:

  /*We use medium quality, ont the iPhone 4 this demo would be laging too much, the conversion in UIImage and CGImage demands too much ressources for a 720p resolution.*/

  [self.captureSession setSessionPreset:AVCaptureSessionPresetMedium];

  其他预置属性如下:

  

  不同设备的情况:

  

  开始编码,视频编码为H.264、音频编码为AAC

   [self performSelector:@selector(startRecording) withObject:nil afterDelay:10.0];

   [self.captureSession startRunning];

 

   - (void) startRecording

   {

       [captureOutput startRecordingToOutputFileURL:[self tempFileURLrecordingDelegate:self];

   }

处理编码过程中事件的类必须符合 VCaptureFileOutputRecordingDelegate 协议,并在以下2个方法中进行处理:

 

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray*)connections

{

    NSLog(@"start record video");

}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error

{

    ALAssetsLibrary *library = [[ALAssetsLibrary allocinit];    

    // 将临时文件夹中的视频文件复制到 照片 文件夹中,以便存取
    [library 
writeVideoAtPathToSavedPhotosAlbum:outputFileURL

                                completionBlock:^(NSURL *assetURL, NSError *error) {

                                    if (error) {

                                        _myLabel.text=@"Error";

                                    }

                                    else

                                        _myLabel.text = [assetURL path];

                                }];

    [library release];}

通过 AVCaptureFileOutput  的 stopRecording 方法停止编码。

 

二、捕获用于处理视频帧

 

三、捕获为静止图像

此时输出设备对象为:AVCaptureStillImageOutput,session 的预置(preset)信息决定图像分辨率:

 

图像格式:

例:

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];

NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];

[stillImageOutput setOutputSettings:outputSettings];

当需要捕获静止图像时,可向输出设备对象发送:captureStillImageAsynchronouslyFromConnection:completionHandler: 消息。第一个参数为欲进行图像捕获的连接对象(AVCaptureConnection),你必须找到具有视频采集输入端口(port)的连接:

AVCaptureConnection *videoConnection = nil;

for (AVCaptureConnection *connection in stillImageOutput.connections) {

     for (AVCaptureInputPort *port in [connection inputPorts]) {

          if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {

               videoConnection = connection;

               break;

          }

     }

     if (videoConnection) { break; }

}

第二个参数是一个块(block),它有2个参数,第一个参数是包含图像数据的  CMSampleBuffer,可用于处理图像:

[[self stillImageOutputcaptureStillImageAsynchronouslyFromConnection:stillImageConnection

        completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

               ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {

                    if (error) {
                        // error handling

                    }

               }

        };

        if (imageDataSampleBuffer != NULL) {

              NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

              ALAssetsLibrary *library = [[ALAssetsLibrary allocinit];  

              UIImage *image = [[UIImage allocinitWithData:imageData];

              // 将图像保存到“照片” 中
              [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation]

              completionBlock:completionBlock];

              [image release];

              [library release];

        }

        else

             completionBlock(nil, error);

        if ([[self delegaterespondsToSelector:@selector(captureManagerStillImageCaptured:)]) {

             [[self delegatecaptureManagerStillImageCaptured:self];

        }

}];

这篇关于iOS自定义相机界面,实现微信小视频UI效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Qt实现对Word网页的读取功能

《Qt实现对Word网页的读取功能》文章介绍了几种在Qt中实现Word文档(.docx/.doc)读写功能的方法,包括基于QAxObject的COM接口调用、DOCX模板替换及跨平台解决方案,重点讨论... 目录1. 核心实现方式2. 基于QAxObject的COM接口调用(Windows专用)2.1 环境

MySQL查看表的历史SQL的几种实现方法

《MySQL查看表的历史SQL的几种实现方法》:本文主要介绍多种查看MySQL表历史SQL的方法,包括通用查询日志、慢查询日志、performance_schema、binlog、第三方工具等,并... 目录mysql 查看某张表的历史SQL1.查看MySQL通用查询日志(需提前开启)2.查看慢查询日志3.

Java实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

MyBatis-Plus逻辑删除实现过程

《MyBatis-Plus逻辑删除实现过程》本文介绍了MyBatis-Plus如何实现逻辑删除功能,包括自动填充字段、配置与实现步骤、常见应用场景,并展示了如何使用remove方法进行逻辑删除,逻辑删... 目录1. 逻辑删除的必要性编程1.1 逻辑删除的定义1.2 逻辑删php除的优点1.3 适用场景2.

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局