1

我有一个 PNG,我正在使用 Objective-c(使用 ImageIO 框架)将其转换为 GIF,但是我的 Alpha 通道被奇怪地转换了。

例如,我有一个红色正方形,正方形顶部有一个光滑的半透明覆盖层,使其看起来像某种斜面。我将图像保存为 PNG,然后在 Objective-c 中尝试将其转换为 GIF。当它被转换时,存在半透明光泽的整个部分变为纯白色(或 UIView 在 UIImage 后面的任何颜色。)

我猜我没有正确设置我的PNG图像的字典值......那么可能的值是什么:

kCGImagePropertyPNGInterlaceType

或者

kCGImagePropertyPNGsRGBIntent

这是我将 gif 转换为动画 gif 然后将其复制到剪贴板的代码(它有效,它只是丢失了我的一些 png 上的 alpha 通道)

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"animated.gif"];
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
                                                                        kUTTypeGIF,
                                                                        16,
                                                                        NULL);


    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
    //[dict setObject:(NSString *) ??? forKey:(NSString *)kCGImagePropertyPNGInterlaceType];
    //[dict setObject:(NSString *) ??? forKey:(NSString *)kCGImagePropertyPNGsRGBIntent];

    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 ];



    CGImageDestinationAddImage(destination, image1.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image2.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image3.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image4.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image5.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image6.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image7.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image8.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image9.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image10.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image11.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image12.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image13.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image14.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image15.image.CGImage, (CFDictionaryRef)frameProperties);
    CGImageDestinationAddImage(destination, image16.image.CGImage, (CFDictionaryRef)frameProperties);

    CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);
    CGImageDestinationFinalize(destination);
    CFRelease(destination);

    NSData *gifData = [[NSData alloc] initWithContentsOfFile:path];

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

    [pasteboard setData:gifData forPasteboardType:@"com.compuserve.gif"];

    [gifData release];
4

0 回答 0