我试图限制动画GIF(从数组创建CGImageRef
)的颜色数量.
但是,我实际上很难设置自定义颜色表.有谁知道如何使用Core Graphics做到这一点?
我知道kCGImagePropertyGIFImageColorMap
.下面是一些测试代码(从这个github gist中大量借用- 因为它是kCGImagePropertyGIFImageColorMap
Google 唯一可以找到的实例).
NSString *path = [@"~/Desktop/test.png" stringByExpandingTildeInPath];
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef)[NSData dataWithContentsOfFile:path]);
CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
const uint8_t colorTable[ 8 ] = { 0, 0, 0, 0, 255, 255, 255 , 255};
NSData* colorTableData = [ NSData dataWithBytes: colorTable length: 8 ];
NSMutableDictionary *gifProps = [NSMutableDictionary dictionary];
[gifProps setObject:colorTableData forKey:(NSString *)kCGImagePropertyGIFImageColorMap];
[gifProps setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
NSDictionary* imgProps = [ NSDictionary dictionaryWithObject: gifProps
forKey: …
Run Code Online (Sandbox Code Playgroud)