3
我使用下面的代碼Xcode的創建GIF的扭曲:的Xcode GIF輸出有顏色,當GIF有大量幀
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, imageArray.count, NULL);
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:dict forKey:(NSString *)kCGImagePropertyPNGDictionary];
NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:5];
const uint8_t colorTable2[9] = {0, 0, 0, 128, 128, 128, 255,255,255};
NSData *colorTableData2 = [NSData dataWithBytes:colorTable2 length:9];
[dict2 setObject:colorTableData2 forKey:(NSString *)kCGImagePropertyGIFImageColorMap];
[dict2 setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
[dict2 setObject:[NSNumber numberWithFloat:0.005] forKey:(NSString *)kCGImagePropertyGIFDelayTime];
NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:dict2 forKey:(NSString *) kCGImagePropertyGIFDictionary];
for (int i = 0; i < imageArray.count; i++) {
UIImage *image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:i]];
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
}
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
CGImageDestinationFinalize(destination);
CFRelease(destination);
這裏的問題是,如果imageArray.count比23更小,輸出GIF將不包含顏色失真。如果它大於22,則會以採用背景顏色而不是原始顏色的圖像塊的形式出現顏色失真。
我不認爲我已經設置了任何代碼來執行圖像壓縮,所以我不知道爲什麼會發生這種情況。如果有人能夠對這個問題提供一些見解,它將對我有很大的幫助。
編輯: 我已經保存了每個單獨的框架以確保顏色失真不是由於框架本身。因此我覺得它與CG框架有關。
謝謝。
嗨吉特,我的問題是,創建的GIF已經失真。直到「UIImage * image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:i]];」一切安好。我可以看到單獨保存的圖像無失真。但是在「CGImageDestinationFinalize(destination);」之後gif文件顯示不正確的顏色。 – Leon
@Leon你曾經爲此找到過解決方案嗎? –