CGImageDestination和文件命名的问题 [英] Problem with CGImageDestination and file naming
问题描述
我从相机捕获图像,使用AVCapture,因为我需要的速度和标准工具包的东西太慢。
我有问题,通过CGImageDestination函数...正在输出的文件(一个动画的GIF)有它的文件名mangles。
I am capturing images from the camera, using AVCapture as I have need of speed and the standard kit stuff is way too slow. I have problem whereby the file that is being output (an animated GIF) is having it's file name mangles by the CGImageDestination functions...
当我输出NSURL(转换为CFURLRef)到日志我得到的路径/文件名我打算:
When I output the NSURL (cast to a CFURLRef) to the log I get the path/filename I intended:
2011-09-04 20:40:25.914 Mover[3558:707] Path as string:.../Documents/91B2C5E8-F925-47F3-B539-15185F640828-3558-000003327A227485.gif
但是,一旦文件被创建并保存,它实际上会列出文件名如下:
However, once the file is created and saved it actually lists the filename as this:
2011-09-04 20:40:25.960 Mover[3558:707] file: .91B2C5E8-F925-47F3-B539-15185F640828-3558-000003327A227485.gif-TtNT
看到区别?开始时的周期和4个字符后缀?
真正的wierd是,它不总是这样做,大约40%的时间工作正常。但是它阻止代码在下面的行中进行工作,我在列表视图中用预览列出它们。
See the difference? the period at the start and the 4 character suffix? Whats really wierd is that it doesn't always do it, about 40% of the time it works OK. However it's preventing the code working further down the line where I'm listing them with previews in a table view.
有人知道为什么,如何停止它吗?
Does anyone know why and how to stop it doing this?
以下是代码:
- (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];
}
推荐答案
AFAIK这是一个临时文件 CGImageDestinationFinalize
,你看到的原因是 CGImageDestinationFinalize
失败。我认为如果你检查文件大小,你会看到那些具有错误名称的文件大小为0。
AFAIK this is a temporary file that CGImageDestinationFinalize
makes, the reason you see them is that CGImageDestinationFinalize
failed. I think that if you check the file sizes you'll see that the ones with mangled names have a file size of 0.
我开始检查成功后,我得到了文件:)
I started check for succes after I got these files :)
bool success = CGImageDestinationFinalize(destination);
CFRelease(destination);
if (success) {
NSLog(@"animated GIF file created at %@", path);
} else {
NSLog(@"failed to create gif at %@", path);
}
这篇关于CGImageDestination和文件命名的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!