尝试 addImage:forDuration:withAttributes 时 QTMovie 索引出界异常
我正在尝试使用 QTKit 在 Cocoa 中创建一部电影。这部电影应该由我创建的一堆 NSImage 组成。我遇到了一些问题。总体问题是这部电影没有制作出来。我得到一个看起来是空的文件,只包含几百个字节,但没有电影可言。不管怎样,代码是:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"Creating .mov out of NSImage(s)");
NSError *error = nil;
// Create empty QTMovie with writable data reference
QTMovie *movie = [[QTMovie alloc] initToWritableFile:@"Temp.mov" error:&error];
// If an error occurred, print it
if ( error )
NSLog(@"Error creating movie: %@", [error localizedDescription]);
// Sanity-check to see that our movie is writable
assert([[movie attributeForKey:QTMovieEditableAttribute] boolValue] == YES);
// Load our image
NSImage *image = [NSImage imageNamed:@"screen-1"];
// Check that our image is loaded
assert(image != nil);
// Add the image to our movie, duration is 6000 / 600 = 10 seconds?
NSDictionary *attributesForImage = [NSDictionary dictionaryWithObjectsAndKeys:@"tiff", QTAddImageCodecType, nil];
[movie addImage:image forDuration:QTMakeTime(6000, 600) withAttributes:attributesForImage];
// Store our movie in a file called "Done.mov"
// flattening it seems like a good idea
NSDictionary *attributesForExport = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],
QTMovieFlatten, nil];
error = nil;
BOOL didSucceed = [movie writeToFile:@"Done.mov" withAttributes:attributesForExport error:&error];
if ( ! didSucceed || error )
NSLog(@"Did not succeed. Error was: %@", [error localizedDescription]);
else
{
// If we did succeed, error should be nil, right?
assert(error == nil);
NSLog(@"Did succeed.");
}
// Cleanup
[movie release];
[[NSFileManager defaultManager] removeItemAtPath:@"Temp.mov" error:nil];
[NSApp terminate:nil];
}
需要关注的行是[movie addImage...
。这就是错误发生的地方。我得到以下例外:
eException', reason: '*** -[NSArray objectAtIndex:]: index (0) beyond bounds (0)'
*** Call stack at first throw:
(
0 CoreFoundation 0x90e806ba __raiseError + 410
1 libobjc.A.dylib 0x97cde509 objc_exception_throw + 56
2 CoreFoundation 0x90ec5321 -[__NSArray0 objectAtIndex:] + 209
3 QTKit 0x9387492c -[QTMovie_QuickTime addImage:forDuration:atTime:withAttributes:] + 708
4 QTKitServer 0x000208ba do_addImageAtTimeWithAttributes + 327
5 QTKitServer 0x0000cb03 _XaddImageAtTimeWithAttributes + 291
6 QTKitServer 0x000021bb QTKitServer_server + 113
7 QTKitServer 0x0001e194 mach_port_callback + 84
8 CoreFoundation 0x90dee772 __CFMachPortPerform + 338
9 CoreFoundation 0x90dea4db __CFRunLoopRun + 6523
10 CoreFoundation 0x90de8464 CFRunLoopRunSpecific + 452
11 CoreFoundation 0x90dee3a4 CFRunLoopRun + 84
12 QTKitServer 0x0001e702 main + 1068
13 QTKitServer 0x00002141 start + 53
)
我用谷歌搜索了很多,但没有找到真正回答这个问题的东西。该代码非常简单,它看起来与大多数代码示例完全相同。图像加载正常,但根本没有添加。有什么想法吗?如果相关的话,它是一个 JPEG 图像。
I am trying to create a movie in Cocoa using QTKit. This movie should consist of a bunch of NSImage's that I have created. I'm running into some problems with this. The overall problem is that the movie isn't created. I get a file which appears to be empty, only contains a couple of hundred bytes, but no movie to speak of. Anyway, the code is:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"Creating .mov out of NSImage(s)");
NSError *error = nil;
// Create empty QTMovie with writable data reference
QTMovie *movie = [[QTMovie alloc] initToWritableFile:@"Temp.mov" error:&error];
// If an error occurred, print it
if ( error )
NSLog(@"Error creating movie: %@", [error localizedDescription]);
// Sanity-check to see that our movie is writable
assert([[movie attributeForKey:QTMovieEditableAttribute] boolValue] == YES);
// Load our image
NSImage *image = [NSImage imageNamed:@"screen-1"];
// Check that our image is loaded
assert(image != nil);
// Add the image to our movie, duration is 6000 / 600 = 10 seconds?
NSDictionary *attributesForImage = [NSDictionary dictionaryWithObjectsAndKeys:@"tiff", QTAddImageCodecType, nil];
[movie addImage:image forDuration:QTMakeTime(6000, 600) withAttributes:attributesForImage];
// Store our movie in a file called "Done.mov"
// flattening it seems like a good idea
NSDictionary *attributesForExport = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],
QTMovieFlatten, nil];
error = nil;
BOOL didSucceed = [movie writeToFile:@"Done.mov" withAttributes:attributesForExport error:&error];
if ( ! didSucceed || error )
NSLog(@"Did not succeed. Error was: %@", [error localizedDescription]);
else
{
// If we did succeed, error should be nil, right?
assert(error == nil);
NSLog(@"Did succeed.");
}
// Cleanup
[movie release];
[[NSFileManager defaultManager] removeItemAtPath:@"Temp.mov" error:nil];
[NSApp terminate:nil];
}
The line to keep an eye on is the [movie addImage...
. This is where the error occurs. I get the following exception:
eException', reason: '*** -[NSArray objectAtIndex:]: index (0) beyond bounds (0)'
*** Call stack at first throw:
(
0 CoreFoundation 0x90e806ba __raiseError + 410
1 libobjc.A.dylib 0x97cde509 objc_exception_throw + 56
2 CoreFoundation 0x90ec5321 -[__NSArray0 objectAtIndex:] + 209
3 QTKit 0x9387492c -[QTMovie_QuickTime addImage:forDuration:atTime:withAttributes:] + 708
4 QTKitServer 0x000208ba do_addImageAtTimeWithAttributes + 327
5 QTKitServer 0x0000cb03 _XaddImageAtTimeWithAttributes + 291
6 QTKitServer 0x000021bb QTKitServer_server + 113
7 QTKitServer 0x0001e194 mach_port_callback + 84
8 CoreFoundation 0x90dee772 __CFMachPortPerform + 338
9 CoreFoundation 0x90dea4db __CFRunLoopRun + 6523
10 CoreFoundation 0x90de8464 CFRunLoopRunSpecific + 452
11 CoreFoundation 0x90dee3a4 CFRunLoopRun + 84
12 QTKitServer 0x0001e702 main + 1068
13 QTKitServer 0x00002141 start + 53
)
I have googled around a lot and found nothing that really answers this question. The code is pretty straightforward, it looks exactly the same as most code samples out there. The image is loaded fine, but it isn't added at all. Any ideas? It's a JPEG-image if that is relevant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码没问题。
[NSImage imageNamed:]
返回的 NSImage 实例似乎存在问题。尝试将该行替换为(假设您的图像已复制到捆绑包的资源文件夹中):
将其添加到电影后,不要忘记添加
[image release]
。Your code is fine. There seems to be a problem with the NSImage instance that is returned by
[NSImage imageNamed:]
.Try to replace that line with (assuming you image is copied into the bundle's resource folder):
Don't forget to add
[image release]
after you added it to your movie.