ios - iphone:尝试将图像导出为电影时,电影已损坏

标签 ios iphone

我正在尝试从单个图像导出电影。 类似于 How do I export UIImage array as a movie? 的内容.

我尝试了很多变体,但我总是收到损坏的视频。 (颜色和图像都是错误的)。 有什么想法吗? 谢谢

代码:

- (void)writeImageAsMovie:(UIImage*)image toPath:(NSString*)path size:(CGSize)size duration:(int)duration 
{
NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSError *error = nil;
[localFileManager removeItemAtPath:path error:&error];
NSLog(@"Write Started");

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                              [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
                                                          error:&error];
NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                               [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                               nil];
AVAssetWriterInput* writerInput = [AVAssetWriterInput
                                    assetWriterInputWithMediaType:AVMediaTypeVideo
                                    outputSettings:videoSettings];

AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                 assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                 sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];

//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

//Write samples:
CVPixelBufferRef buffer = [self newPixelBufferFromCGImage:image.CGImage andSize:image.size];
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(duration-1, 2)];

//Finish the session:
[writerInput markAsFinished];
[videoWriter endSessionAtSourceTime:CMTimeMake(duration, 2)];
[videoWriter finishWriting];

- (CVPixelBufferRef) newPixelBufferFromCGImage: (CGImageRef) image andSize:(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, 
                                             kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
//CGContextConcatCTM(context, frameTransform);
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
                                       CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);

CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

return pxbuffer;

最佳答案

此处仅供引用,但当正在编码的视频的宽度为奇数时,我在模拟器中看到了同样的错误。例如,在 16x16 的图像上没有错误,但在 17x16 的电影中会出现错误。它不会出现在 18x18 电影中,所以我认为它与期望输入是 4 的倍数的 H264 编码过程有关。此外,这似乎只是模拟器中的问题。

关于ios - iphone:尝试将图像导出为电影时,电影已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9979694/

相关文章:

ios - 使用其键值将数组中的数据添加到另一个数组

iphone - 在没有App Store的情况下将App分发给大型但外部成员

ios - 苹果手机 : why my in-app purchase product array object index set automatically in alphabetical order

ios - 以 AAC 格式录制的 xCode 无法正常工作

iphone - 如何防止 subview 与标签栏重叠?

ios - 使用 Google SpreadSheet API 更新 iOS Swift 中的特定行

ios - iPhone CATextLayer 不显示其文本

iphone - SQLite iPhone数据插入问题

ios - 保证 iOS 何时写入 Documents 目录中的文件?

ios - 将 subview 添加到 UITableViewController