我的 AVAssetWriter
在使用 AVAssetWriterInputPixelBufferAdaptor
向其附加 5 个左右的图像后失败,我不知道为什么。
这个热门问题有所帮助,但无法满足我的需求:
How do I export UIImage array as a movie?
一切都按计划进行,我什至延迟assetWriterInput,直到它可以处理更多媒体。 但由于某种原因,它总是在 5 张左右的图像后失败。我使用的图像是从 GIF 中提取的帧
这是我的迭代代码:
-(void)writeImageData
{
__block int i = 0;
videoQueue = dispatch_queue_create("com.videoQueue", DISPATCH_QUEUE_SERIAL);
[self.writerInput requestMediaDataWhenReadyOnQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) usingBlock:^{
while (self.writerInput.readyForMoreMediaData) {
if (i >= self.imageRefs.count){
[self endSession];
videoQueue = nil;
[self saveToLibraryWithCompletion:^{
NSLog(@"Saved");
}];
break;
}
if (self.writerInput.readyForMoreMediaData){
CGImageRef imageRef = (__bridge CGImageRef)self.imageRefs[i];
CVPixelBufferRef buffer = [self pixelBufferFromCGImageRef:imageRef];
CGFloat timeScale = (CGFloat)self.imageRefs.count / self.originalDuration;
BOOL accepted = [self.adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(i, timeScale)];
CVBufferRelease(buffer);
if (!accepted){
NSLog(@"Buffer did not add %@, index %d, timescale %f", self.writer.error, i, timeScale);
}else{
NSLog(@"Buffer did nothing wrong");
}
i++;
}
}
}];
}
我的其他代码与上面链接中的代码相匹配。这只是略有不同:
-(CVPixelBufferRef)pixelBufferFromCGImageRefCGImageRef)image
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVPixelBufferRef pxbuffer = NULL;
CGFloat width = 640;
CGFloat height = 640;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width,
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, width,
height, 8, 4*width, rgbColorSpace,
kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextDrawImage(context, CGRectMake(0, 0, width,
height), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}
让我印象深刻的一件事是您对 CMTimeMake(adjustedTime, 1)
的使用。
您需要正确计算每一帧的时间。请注意,CMTime
采用两个整数,并将它们作为浮点值传递会截断它们。
第二个问题是您没有使用串行调度队列
关于ios - UIImages 导出为电影错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23839149/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |