2011-09-04 109 views
0

我從相機捕獲圖像,使用AVCapture,因爲我需要速度,標準套件的速度太慢。 我有問題,其中正在輸出的文件(動畫GIF)是由CGImageDestination函數的文件名損壞...CGImageDestination和文件命名問題

當我輸出NSURL(強制轉換爲CFURLRef)到日誌我得到路徑/文件名我打算:

2011-09-04 20:40:25.914 Mover[3558:707] Path as string:.../Documents/91B2C5E8-F925-47F3-B539-15185F640828-3558-000003327A227485.gif 

然而,一旦文件被創建並保存它實際上列出了文件名,因爲這:

2011-09-04 20:40:25.960 Mover[3558:707] file: .91B2C5E8-F925-47F3-B539-15185F640828-3558-000003327A227485.gif-TtNT 

看到區別?開始的時間和4個字符的後綴? 真的很奇怪的是,它不總是做到這一點,約40%的時間,它的工作確定。但是,它阻止了代碼進一步工作,我在列表視圖中列出預覽。

有誰知道爲什麼以及如何阻止它做到這一點?

下面的代碼:

- (void)exportAnimatedGif{ 

NSString *guidPath = [[NSProcessInfo processInfo] globallyUniqueString]; 
NSString *tmpPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:guidPath]; 
NSString *path = [tmpPath stringByAppendingPathExtension:@"gif"]; 
NSLog(@"Path as string:%@", path); 
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], kUTTypeGIF, [captureArray count], NULL); 

NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:testDisplay3.displayValue]         forKey:(NSString *)kCGImagePropertyGIFDelayTime] 
                  forKey:(NSString *)kCGImagePropertyGIFDictionary]; 
NSDictionary *gifProperties = [NSDictionary dictionaryWithObject: 
           [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:2], (NSString *)kCGImagePropertyGIFLoopCount, 
           [NSNumber numberWithFloat:testDisplay3.displayValue], (NSString*)kCGImagePropertyGIFDelayTime, 
           [NSNumber numberWithFloat:testDisplay3.displayValue], (NSString*)kCGImagePropertyGIFUnclampedDelayTime, 
           nil] 
                  forKey:(NSString *)kCGImagePropertyGIFDictionary]; 
for (int ii = 0; ii < [captureArray count]; ii++) 
{ 
    UIImage *tmpImg = [[UIImage alloc] init]; 
    tmpImg = [captureArray objectAtIndex:ii]; 
    CGImageDestinationAddImage(destination, tmpImg.CGImage, (CFDictionaryRef)frameProperties); 
} 
CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties); 
CGImageDestinationFinalize(destination); 
CFRelease(destination); 

//TEST OUTPUT GENERATED FILES 
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] error:nil]; 
for (int xx = 0; xx < [contents count]; xx++) 
{ 
    NSLog(@"file: %@", [contents objectAtIndex:xx]); 
} 
//END TEST CODE 
[captureArray removeAllObjects]; 
} 
+0

添加調試行以查看從NSSearchPathForDirectoriesInDomains'返回的路徑數您正在訪問此方法兩次,第二次可能會返回不同的過時路徑。這個函數可以返回多個路徑,我不認爲他們會以什麼順序保證。嘗試訪問第一個對象而不是最後一個。另外,請確保您在開始時只訪問此功能一次,並將路徑存儲在ivar中。這樣你就可以保證兩次獲得相同的路徑。 –

回答

1

據我所知,這是一個臨時文件,CGImageDestinationFinalize做,你看他們是CGImageDestinationFinalize失敗的原因。我認爲,如果你檢查文件大小,你會看到有錯位的名稱的那些具有0

的文件大小,我開始檢查更迭後,我得到了這些文件:)

bool success = CGImageDestinationFinalize(destination); 
CFRelease(destination); 
if (success) { 
    NSLog(@"animated GIF file created at %@", path); 
} else { 
    NSLog(@"failed to create gif at %@", path); 
}