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

相关文章

Java实现XML与JSON的互相转换详解

《Java实现XML与JSON的互相转换详解》这篇文章主要为大家详细介绍了如何使用Java实现XML与JSON的互相转换,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. XML转jsON1.1 代码目的1.2 代码实现2. JSON转XML3. JSON转XML并输出成指定的

利用Python实现添加或读取Excel公式

《利用Python实现添加或读取Excel公式》Excel公式是数据处理的核心工具,从简单的加减运算到复杂的逻辑判断,掌握基础语法是高效工作的起点,下面我们就来看看如何使用Python进行Excel公... 目录python Excel 库安装Python 在 Excel 中添加公式/函数Python 读取

Python实现合并与拆分多个PDF文档中的指定页

《Python实现合并与拆分多个PDF文档中的指定页》这篇文章主要为大家详细介绍了如何使用Python实现将多个PDF文档中的指定页合并生成新的PDF以及拆分PDF,感兴趣的小伙伴可以参考一下... 安装所需要的库pip install PyPDF2 -i https://pypi.tuna.tsingh

基于Python和Tkinter实现高考倒计时功能

《基于Python和Tkinter实现高考倒计时功能》随着高考的临近,每个考生都在紧锣密鼓地复习,这时候,一款实用的倒计时软件能有效帮助你规划剩余时间,提醒你不要浪费每一分每一秒,今天,我们来聊聊一款... 目录一、软件概述:二、功能亮点:1. 高考倒计时2. 添加目标倒计时3. 励志语句4. 透明度调节与

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

Python实现PDF与多种图片格式之间互转(PNG, JPG, BMP, EMF, SVG)

《Python实现PDF与多种图片格式之间互转(PNG,JPG,BMP,EMF,SVG)》PDF和图片是我们日常生活和工作中常用的文件格式,有时候,我们可能需要将PDF和图片进行格式互转来满足... 目录一、介绍二、安装python库三、Python实现多种图片格式转PDF1、单张图片转换为PDF2、多张图

Java实现数据库图片上传与存储功能

《Java实现数据库图片上传与存储功能》在现代的Web开发中,上传图片并将其存储在数据库中是常见的需求之一,本文将介绍如何通过Java实现图片上传,存储到数据库的完整过程,希望对大家有所帮助... 目录1. 项目结构2. 数据库表设计3. 实现图片上传功能3.1 文件上传控制器3.2 图片上传服务4. 实现

Java实现MD5加密的四种方式

《Java实现MD5加密的四种方式》MD5是一种广泛使用的哈希算法,其输出结果是一个128位的二进制数,通常以32位十六进制数的形式表示,MD5的底层实现涉及多个复杂的步骤和算法,本文给大家介绍了Ja... 目录MD5介绍Java 中实现 MD5 加密方式方法一:使用 MessageDigest方法二:使用

mysql删除无用用户的方法实现

《mysql删除无用用户的方法实现》本文主要介绍了mysql删除无用用户的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 1、删除不用的账户(1) 查看当前已存在账户mysql> select user,host,pa

Nginx配置location+rewrite实现隐性域名配置

《Nginx配置location+rewrite实现隐性域名配置》本文主要介绍了Nginx配置location+rewrite实现隐性域名配置,包括基于根目录、条件和反向代理+rewrite配置的隐性... 目录1、配置基于根目录的隐性域名(就是nginx反向代理)2、配置基于条件的隐性域名2.1、基于条件