2011-06-07 81 views
3

我正在嘗試使用QTKit在Cocoa中創建電影。這部電影應該由我創建的一堆NSImage組成。我遇到了一些問題。總體問題是電影沒有創建。我看到一個文件似乎是空的,只包含幾百個字節,但沒有電影可言。總之,代碼:QTMovie嘗試添加圖像時出現異常的異常索引:forDuration:withAttributes

- (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圖像,如果這是相關的。

+0

你已經說過這是一個JPEG圖像,但是你在你的屬性字典中將'QTAddImageCodecType'設置爲'tiff'。這可能與你的問題有關。 – 2011-06-08 02:48:36

回答

2

您的代碼沒問題。由[NSImage imageNamed:]返回的NSImage實例似乎有問題。
嘗試更換該行(假設你的圖像複製到該包的資源文件夾):

NSString* imagePath = [[[NSBundle mainBundle] resourcePath]  
         stringByAppendingPathComponent:@"screen-1.jpg"]; 
NSImage* image = [[NSImage alloc] initWithContentsOfFile:imagePath]; 

不要忘記添加[image release]您添加到您的電影。

+1

這就是解決方案!但不是你的版本,我做了:NSData * data = [image TIFFRepresentation]; NSImage * image2 = [[NSImage alloc] initWithData:data];哪些工作。這個問題很奇怪。任何想法有什麼錯誤的形象? – ErikPerik 2011-06-08 12:25:23

+2

我在蘋果提交了一個bug。看起來QTKit絆倒了圖像名稱元數據,該元數據被寫入通過[NSImage imageNamed:]加載的任何NSImage對象。 – 2011-06-08 13:43:24