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

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测

pytorch自动求梯度autograd的实现

《pytorch自动求梯度autograd的实现》autograd是一个自动微分引擎,它可以自动计算张量的梯度,本文主要介绍了pytorch自动求梯度autograd的实现,具有一定的参考价值,感兴趣... autograd是pytorch构建神经网络的核心。在 PyTorch 中,结合以下代码例子,当你

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各