【问题标题】:Creating GIF from UIImages with CGImageDestinationRef results in broken line at bottom使用 CGImageDestinationRef 从 UIImages 创建 GIF 会导致底部出现虚线
【发布时间】:2016-05-04 17:14:51
【问题描述】:

我尝试用图像创建 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,但图像看起来像这样:

【问题讨论】:

    标签: ios objective-c uiimage gif


    【解决方案1】:

    啊哈,我修复了这个错误

    你的图像由

    创建或修改

    UIGraphicsBeginImageContextWithOptions(大小、不透明、缩放);

    只需将 不透明 设置为 NO !尽情享受吧

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      相关资源
      最近更新 更多