ios - UIImages 导出为电影错误
<p><h1>问题</h1>
<p>我的 <code>AVAssetWriter</code> 在使用 <code>AVAssetWriterInputPixelBufferAdaptor</code> 向其附加 5 个左右的图像后失败,我不知道为什么。</p>
<h1>详情</h1>
<p>这个热门问题有所帮助,但无法满足我的需求:</p>
<p> <a href="https://stackoverflow.com/questions/3741323/how-do-i-export-uiimage-array-as-a-movie" rel="noreferrer noopener nofollow">How do I export UIImage array as a movie?</a> </p>
<p>一切都按计划进行,我什至延迟assetWriterInput,直到它可以处理更多媒体。
但由于某种原因,它总是在 5 张左右的图像后失败。我使用的图像是从 GIF 中提取的帧</p>
<h1>代码</h1>
<p>这是我的迭代代码:</p>
<pre><code>-(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){
;
videoQueue = nil;
[self saveToLibraryWithCompletion:^{
NSLog(@"Saved");
}];
break;
}
if (self.writerInput.readyForMoreMediaData){
CGImageRef imageRef = (__bridge CGImageRef)self.imageRefs;
CVPixelBufferRef buffer = ;
CGFloat timeScale = (CGFloat)self.imageRefs.count / self.originalDuration;
BOOL accepted = ;
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++;
}
}
}];
</code></pre>
<p>}</p>
<p>我的其他代码与上面链接中的代码相匹配。这只是略有不同:</p>
<pre><code>-(CVPixelBufferRef)pixelBufferFromCGImageRef:(CGImageRef)image
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
, kCVPixelBufferCGImageCompatibilityKey,
, 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;
</code></pre>
<p>}</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>让我印象深刻的一件事是您对 <code>CMTimeMake(adjustedTime, 1)</code> 的使用。</p>
<p>您需要正确计算每一帧的时间。请注意,<code>CMTime</code> 采用两个整数,并将它们作为浮点值传递会截断它们。</p>
<p>第二个问题是您没有使用串行调度队列:)</p></p>
<p style="font-size: 20px;">关于ios - UIImages 导出为电影错误,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/23839149/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/23839149/
</a>
</p>
页:
[1]