图片合成视频

https://github.com/czboosj/HJImagesToVideo 自己修复了这个库的内存泄漏

https://github.com/Willib/ImagesToVideo
swift 版本的图片处理

https://stackoverflow.com/questions/3741323/how-do-i-export-uiimage-array-as-a-movie?noredirect=1&lq=1
图片合成详解

From: http://www.itqls.com/index.php?m=Home&c=Article&a=index&id=63

最近做一个无线设备相关的项目,

需要把扫描得到的图像,合成视频.

由于扫描得到的图像 是通过 drawRect 绘图画出来的,不是一个 Image

而图片转视频的方法是需要用 CGImage去实现的.

方法如下:

由于我需要得到 Image 数组, 就需要把我绘制出来的图片转换一下,

截屏: (正确姿势)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (UIImage*)screenCap
{
    CGSize size=  CGSizeMake((int)self.bounds.size.width, (int)self.bounds.size.height);
    
//  size 为尺寸, YES 为不透明,第三个参数是缩放比例,直接根据屏幕设置保持清晰度
//  关于这个不透明好多博客都说错了,opaque 的意思是不透明...
    UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
// 遇到这个没拆分出来直接写,后发现内存泄漏,所以提出来 然后Relese
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:context];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
CGContextRelease(context);
    UIGraphicsEndImageContext();
    return viewImage;
}

通过这个方法可以拿到当前屏幕的图像转换成的 Image

但是,如果需要拿到大量的图,比如100张,他就相当于去截图100张,

内存消耗会非常恐怖.

所以 调用 以上方法 screenCap 的时候, 在外面包一层 autoRelease 去解决.

图片转视频:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
- (void)saveVideo:(NSMutableArray *)imageArr withPaths:(NSString *)paths andCallBack:(void(^)(void))callBack
{
    if (!imageArr.count) {
        return;
    }
    
    UIImage *image = self.screenCap;
    
    CGSize sizeImage = image.size;
    int width = ((int) (sizeImage.width / 16) * 16);
    int height = ((int) (sizeImage.height / 16) * 16);
    NSLog(@"%d,%d",width,height);
    
    CGSize size = CGSizeMake(width, height);
    
    NSError *error = nil;
    unlink([paths UTF8String]);
    
    AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                  [NSURL fileURLWithPath:paths]
                                fileType:AVFileTypeQuickTimeMovie
                                error:&error];
    
    NSParameterAssert(videoWriter);
    
    if(error)
        
        NSLog(@"error = %@", [error localizedDescription]);
    
    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,
                                   [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                                   [NSNumber numberWithInt:size.height], AVVideoHeightKey, nil];
    
    AVAssetWriterInput *writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
    
    
    NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                           
                                                           [NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];
    
    
    
    AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                            sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
    
    NSParameterAssert(writerInput);
    
    NSParameterAssert([videoWriter canAddInput:writerInput]);
    
    if ([videoWriter canAddInput:writerInput])
        NSLog(@"I can add this input");
    else
        NSLog(@"i can't add this input");
    
    [videoWriter addInput:writerInput];
    
    [videoWriter startWriting];
    
    [videoWriter startSessionAtSourceTime:kCMTimeZero];
    dispatch_queue_t    dispatchQueue = dispatch_queue_create("mediaInputQueue", NULL);
    
    [writerInput requestMediaDataWhenReadyOnQueue:dispatchQueue usingBlock:^{
            
        CVPixelBufferRef buffer = NULL;
        NSUInteger fps = 10;
        int frameCount = 0;
        double numberOfSecondsPerFrame = 0.1;
        double frameDuration = fps * numberOfSecondsPerFrame;
        
        for (UIImage *img in imageArr)
        {
            //convert uiimage to CGImage.
            buffer = [self pixelBufferFromCGImage:[img CGImage] size:size];
            
            BOOL append_ok = NO;
            int j = 0;
            while (!append_ok && j < 30)
            {
                if (adaptor.assetWriterInput.readyForMoreMediaData)
                {
                    CMTime frameTime = CMTimeMake(frameCount * frameDuration, (int32_t) fps);
                    append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
                    if (!append_ok)
                    {
                        NSError *error = videoWriter.error;
                        if (error != nil)
                        {
                            NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
                        }
                    }
                }
                else
                {
                    printf("adaptor not ready %d, %d\n", frameCount, j);
                    [NSThread sleepForTimeInterval:0.1];
                }
                j++;
            }
            if (!append_ok)
            {
                printf("error appending image %d times %d\n, with error.", frameCount, j);
            }
            frameCount++;
            
            CVPixelBufferRelease(buffer);
        }
        //Finish the session:
        [writerInput markAsFinished];
        [videoWriter finishWriting];
        
        callBack();
    }];
    
    NSLog(@"outside for loop");
    
}

下面这个方法是拿到 一个 buffer 的 , 通过它去拼接视频.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
- (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image size:(CGSize)size
{
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                             [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
    CVPixelBufferRef pxbuffer = NULL;
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width, size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options, &pxbuffer);
    
    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
    
    CVPixelBufferLockBaseAddress(pxbuffer, 0);
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
    NSParameterAssert(pxdata != NULL);
    
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pxdata, size.width, size.height, 8, 4 * size.width, rgbColorSpace, kCGImageAlphaPremultipliedFirst);
    NSParameterAssert(context);
    
    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
    
    CGColorSpaceRelease(rgbColorSpace);
    CGContextRelease(context);
    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
    return pxbuffer;
}

这里会有一个内存飙升的问题,所以要注意 release.

Author

陈昭

Posted on

2017-05-15

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.