CGImageDestination和文件命名问题

时间:2011-09-04 19:46:45

标签: iphone cocoa-touch cocoa ios4 core-graphics

我正在使用AVCapture从相机捕捉图像,因为我需要速度,标准套件的东西太慢了。 我有问题,正在输出的文件(动画GIF)通过CGImageDestination函数使其文件名为mangles ...

当我将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];
}

1 个答案:

答案 0 :(得分:1)

AFAIK这是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);
}