在IOS视频处理中,视频分解图片和图片合成视频是IOS视频处理中经常遇到的问题,这篇博客就这两个部分对IOS视频图像的相互转换做一下分析。

(1)视频分解图片

这里视频分解图片使用的是AVAssetImageGenerator,利用这个class可以很方便的实现不同时间戳下,视频帧的抓取。注意一般这种视频分解图片帧的方法都是放在子线程中的,而UI更新操作都是放在主线程中的。下面来看看核心代码:

_imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:_asset];

images = [[NSMutableArray alloc]initWithCapacity:10];

_imageGenerator.maximumSize = THUMBNAIL_SIZE;

CMTime duration = _asset.duration;

CMTimeValue intervalSeconds = duration.value / 3;

CMTime time = kCMTimeZero;

NSMutableArray *times = [NSMutableArray array];

for (NSUInteger i = 0; i < 3; i++) {

[times addObject:[NSValue valueWithCMTime:time]];

time = CMTimeAdd(time, CMTimeMake(intervalSeconds, duration.timescale));

}

[_imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime,                                                                                      CGImageRefcgImage,

CMTime actualTime,

AVAssetImageGeneratorResult result,

NSError *error) {

if (cgImage) {

UIImage *image = [UIImage imageWithCGImage:cgImage];

[images addObject:image];

}

if (images.count == 3) {

dispatch_async(dispatch_get_main_queue(), ^{

self.imageview1.image = [images objectAtIndex:0];

self.imageview2.image = [images objectAtIndex:1];

self.imageview3.image = [images objectAtIndex:2];

});

}

}];

分解之后的帧效果如下:                               图片合成视频效果如下:

(2)图片合成视频

图片合成视频的方法相对来说更加复杂一点,我们主要用到的class是这个:

AVAssetWriterInputPixelBufferAdaptor。不同之处在于这里我们还要设置图片合成视频的各种参数,比如帧率,编码方式等等。

2.1 设置文件封装类型

AVFileTypeQuickTimeMovie

2.2 设置图片格式

kCVPixelFormatType_32ARGB

2.3 设置编码方式、图片尺寸

NSDictionary *videoSettings = @{AVVideoCodecKey : AVVideoCodecH264,

AVVideoWidthKey : [NSNumber numberWithInt:(int)width],

AVVideoHeightKey : [NSNumber numberWithInt:(int)height]};

2.4 图片合成开始

CMTime lastTime = CMTimeMake(i, self.frameTime.timescale);

CMTime presentTime = CMTimeAdd(lastTime, self.frameTime);

[self.bufferAdapter appendPixelBuffer:sampleBuffer withPresentationTime:presentTime];

IOS 视频分解图片、图片合成视频的更多相关文章

  1. opencv 将视频分解成图片和使用本地图片合成视频

    代码如下: // cvTest.cpp : Defines the entry point for the console application. #include "stdafx.h&q ...

  2. opencv学习之路(3)、批量读取图片、视频分解、视频合成

    一.批量有序读取图片 #include<opencv2/opencv.hpp> using namespace cv; void main() { //批量读取图片(有序) ]; ]; M ...

  3. 在Ubuntu14.04下安装 ffmpeg-2.4.13(处理视频用,将视频保存为图片序列)

    首先在 http://www.ffmpeg.org/olddownload.html 下载 ffmpeg-2.4.13.tar.bz2 : 然后安装 yasm 和 libx264: apt-get i ...

  4. python视频与帧图片的相互转化,以及查看视频分辨率

    1.拆分视频为帧图片 import cv2 def video2frame(videos_path,frames_save_path,time_interval): vidcap = cv2.Vide ...

  5. iOS 解决LaunchScreen中图片加载黑屏问题

    iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...

  6. iOS根据Url 获取图片尺寸

    iOS根据Url 获取图片尺寸 // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if([i ...

  7. IOS第六天(3:scrollView 图片轮播器)

    IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...

  8. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

  9. ios里面如何压缩图片

    在iOS里面,压缩图片跟在其他环境里面差不多,都和累死, 就是对当前图片从新画图,制定一个尺寸的问题 UIImage* image = [UIImage imageNamed:@"cat.j ...

随机推荐

  1. label swift

    var label = UILabel(frame: CGRectMake(10, 20, 300, 100)) label.text = "<Swift语言实战入门>" ...

  2. Redis基础教程

    说明:本文中涉及的代码是c#所写,连接redis的第三方驱动为ServiceStack.Redis.连接redis的客户端软件为redis-desktop-manager. 一.Redis是什么 Re ...

  3. 提取html中的src 路径

    /// <summary> /// 替换body中的img src属性 附加上域名 /// </summary> /// <param name="str&qu ...

  4. ReactJS入门

    React介绍 React是facebook开发基于组件驱动开发(CDD ) 的UI类库,相对于双向绑定的类库,如AngularJS,它采用单向数据流绑定.通过采用虚拟DOM的概念,是的他在性能和处理 ...

  5. 修改placeholder文字颜色

    .area_ipt ::-webkit-input-placeholder { /* WebKit browsers */ color:#258aca; } .area_ipt :-moz-place ...

  6. HDU 5151 Sit sit sit 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5151 题解: 有n个椅子,编号为1到n. 现在有n个同学,编号为1到n,从第一个同学开始选择要坐的位 ...

  7. javaScript中eval()方法转换json对象

    <script language="javascript"> var user = '{name:"张三",age:23,'+ 'address:{ ...

  8. ios 5

    1.屏幕尺寸568×2/320×2  需要一张568h@2x.png的图片. 2.iOS5不支持udid,用uuid替代,取得uuid方法: -(NSString*) uuid { CFUUIDRef ...

  9. cts 测试环境安装 ubuntu

    1 下载cts测试包 和 sdk 包 http://source.android.com/compatibility/downloads.html  ----cts 包 http://develope ...

  10. 强力重置ASP.NET membership加密后的密码![转]

    公司网站的用户管理采用的是ASP.NET内置的membership管理,在web.config文件中的密码格式配置是加密了的,passwordFormat="Hashed",这样在 ...