如何从一系列生成的图像创建 Quicktime 影片?
我需要从一系列生成的图像创建一部电影。 (我正在根据物理建模程序的输出创建图像。)
我在 QtKitCreateMovie 中找到了 Apple 的示例,并将其用作起点。我不是从应用程序包加载 jpg,而是绘制到 NSImage,然后将该 NSImage 添加到电影对象。这是我用于测试的基本代码。 mMovie
是 QTMovie
的一个实例:
NSImage *anImage = [[NSImage alloc] initWithSize:NSMakeSize(frameSize, frameSize)];
[anImage lockFocus];
float blendValue;
for (blendValue = 0.0; blendValue <= 1.0; blendValue += 0.05) {
[[[NSColor blueColor] blendedColorWithFraction:blendValue ofColor:[NSColor redColor]] setFill];
[NSBezierPath fillRect:NSMakeRect(0, 0, frameSize, frameSize)];
[mMovie addImage:anImage forDuration:duration withAttributes:myDict];
}
[anImage unlockFocus];
[anImage release];
它在 OS X 10.5 下工作,但在 OS X 10.6 下我得到一个数组索引超出范围异常调用 addImage:forDuration:withAttributes
: (http://openradar.appspot .com/radar?id=1146401)
在 10.6 下创建电影的正确方法是什么?
另外,虽然这在 10.5 下有效,但如果我尝试创建具有数千帧的电影,我就会耗尽内存。这也让我觉得我没有使用正确的方法。
I need to create a movie from a series of generated images. (I'm creating the images based on the output of a physics modeling program.)
I found Apple's sample in QtKitCreateMovie and used that as a starting point. Instead of loading jpgs from the application bundle, I'm drawing to an NSImage and then adding that NSImage to the movie object. Here's the basic code I used for testing. mMovie
is an instance of QTMovie
:
NSImage *anImage = [[NSImage alloc] initWithSize:NSMakeSize(frameSize, frameSize)];
[anImage lockFocus];
float blendValue;
for (blendValue = 0.0; blendValue <= 1.0; blendValue += 0.05) {
[[[NSColor blueColor] blendedColorWithFraction:blendValue ofColor:[NSColor redColor]] setFill];
[NSBezierPath fillRect:NSMakeRect(0, 0, frameSize, frameSize)];
[mMovie addImage:anImage forDuration:duration withAttributes:myDict];
}
[anImage unlockFocus];
[anImage release];
This works under OS X 10.5, but under OS X 10.6 I get an array index beyond bounds exception on the call to addImage:forDuration:withAttributes
: (http://openradar.appspot.com/radar?id=1146401)
What's the proper way to create a movie under 10.6?
Also, although this works under 10.5, I run out of memory if I try to create a movie with thousands of frames. That also makes me think I'm not using the correct approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你做对了,但你做错了。
QTKit 中正确的方法没有改变。您的错误是您试图在完成图像之前添加图像,这种情况在您解锁焦点时发生。由于您直到尝试添加图像(20 次)后才解锁焦点,因此您尝试添加未完成的图像(20 次),但这是行不通的。
“越界”例外是因为图像没有表示。 QTMovie 似乎正在尝试循环遍历图像返回的数组以响应
representations
消息,但该数组为空,因为图像尚未完成。不知何故,你在 Leopard 中逃脱了这个错误(可能是因为 Snow Leopard 中的实现细节发生了变化),但我想说这同样是你的错误。
解决方案很简单,每次循环时锁定焦点并解锁图像上的焦点:
You're doing it right, but you're doing it wrong.
The correct way hasn't changed in QTKit. Your mistake is that you're trying to add the image before you have finished it, which happens when you unlock focus. Since you don't unlock focus until after you try to add the image (20 times), you are trying to add an unfinished image (20 times), which doesn't work.
The “out of bounds” exception is because the image has no representations. QTMovie, it seems, is trying to loop through the array returned by the image in response to a
representations
message, but that array is empty because the image is not finished.Somehow, you got away with this in Leopard (probably due to an implementation detail that changed in Snow Leopard), but I'd say it was no less your bug then.
The solution is simply to lock focus and unlock focus on the image each time through the loop: