首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试添加index :forDuration:withAttributes时的出界异常的QTMovie索引

尝试添加index :forDuration:withAttributes时的出界异常的QTMovie索引
EN

Stack Overflow用户
提问于 2011-06-08 04:35:55
回答 1查看 413关注 0票数 3

我正在尝试使用QTKit在可可中创建一部电影。这部电影应该由我创建的一堆NSImage组成。我在这方面遇到了一些问题。总体问题是电影并没有被创造出来。我得到一个文件,它看起来是空的,只包含几百个字节,但没有电影。不管怎样,代码是:

代码语言:javascript
复制
- (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...。这就是错误发生的地方。我得到了以下异常:

代码语言:javascript
复制
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图像,如果这是相关的话。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-08 17:17:13

您的代码很好。[NSImage imageNamed:]返回的NSImage实例似乎有问题。

尝试将该行替换为(假设您的映像被复制到包的资源文件夹中):

代码语言:javascript
复制
NSString* imagePath = [[[NSBundle mainBundle] resourcePath]     
                      stringByAppendingPathComponent:@"screen-1.jpg"];
NSImage* image = [[NSImage alloc] initWithContentsOfFile:imagePath];

在将[image release]添加到电影中后,不要忘记添加它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6271302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档