将PNG转换为GIF - Alpha通道已损坏

时间:2013-01-21 21:27:20

标签: ios objective-c png gif javax.imageio

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

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

我猜我没有正确设置我的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];

0 个答案:

没有答案