使用CGImageDestinationRef从UIImages创建GIF会导致底部出现虚线

时间:2016-01-27 16:23:30

标签: ios objective-c uiimage gif

尝试使用图片创建GIF的代码:

+ (NSData *)createGifDataWithImages:(NSArray *)images duration:(CGFloat)duration withFileName:(NSString *)fileName
{
    CGFloat durationPadding = duration/((CGFloat)images.count);

    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:fileName];

    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
                                                                        kUTTypeGIF,
                                                                        images.count,
                                                                        NULL);

    NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:@(durationPadding) forKey:(NSString *)kCGImagePropertyGIFDelayTime]
                                                                forKey:(NSString *)kCGImagePropertyGIFDictionary];
    NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
                                                              forKey:(NSString *)kCGImagePropertyGIFDictionary];

    [images enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImage *image = (UIImage *)obj;
        CGImageDestinationAddImage(destination, image.CGImage, (CFDictionaryRef)frameProperties);
    }];

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

    NSLog(@"animated GIF file created at %@", path);

    return [NSData dataWithContentsOfFile:path];
}

它确实创建了一个GIF,但图像看起来像这样:

enter image description here

0 个答案:

没有答案