1

我正在使用面向 Mac OS 的 swift 导出动画 gif,它可以工作,但我设置的属性没有被应用。我已经设置了我的属性,当我记录它们时,它们显示如下:

{
HasGlobalColorMap = 1;
LoopCount = 0;
}

在一个循环中,我将每一帧添加到目标:

CGImageDestinationAddImage(destination, cgGif, gifProperties as CFDictionaryRef)

然后在循环之后,我尝试再次设置属性并最终确定目标:

CGImageDestinationSetProperties(destination, gifProperties as CFDictionaryRef)
            if (!CGImageDestinationFinalize(destination)) {
            NSLog("failed to finalize image destination")
        }

但是,生成的图像未设置为循环。为什么会这样?

4

1 回答 1

5

其实我已经进步了...

我正在设置这样的选项:

        var imageProperties:NSMutableDictionary = NSMutableDictionary()
    imageProperties.setObject(NSNumber(float: 0.1), forKey: kCGImagePropertyGIFDelayTime as NSString)
    imageProperties.setObject(NSNumber(bool: false), forKey: kCGImagePropertyHasAlpha as NSString)
    imageProperties.setObject(NSString(string: "kUTTypeGIF"), forKey: kCGImageSourceTypeIdentifierHint as NSString)
    imageProperties.setObject(NSNumber(bool: false), forKey: kCGImagePropertyGIFImageColorMap as NSString)
    imageProperties.setObject(NSNumber(bool: true), forKey: kCGImageSourceShouldCache as NSString)
    let nestedImageProperties:NSDictionary = [imageProperties: kCGImagePropertyGIFDictionary]
    println("nestedImageProperties: \(nestedImageProperties)")

这给了我这个结构:

nestedImageProperties: {
    {
    DelayTime = "0.1";
    HasAlpha = 0;
    ImageColorMap = 0;
    kCGImageSourceShouldCache = 1;
    kCGImageSourceTypeIdentifierHint = kUTTypeGIF;
} = "{GIF}";
}

我找到了一个定义如下属性的实现

let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 1.0]]

给出这个结构:

Frame Properties: [{GIF}: [DelayTime: 1.0]]
File Properties: [{GIF}: [LoopCount: 0]]

然后我将 fileProperties 应用到目的地:

CGImageDestinationSetProperties(destination, fileProperties as CFDictionaryRef)

以及添加每个图像的 frameProperties:

CGImageDestinationAddImage(destination, imageRef, frameProperties as CFDictionaryRef)

最后,它正在起作用,它们正在被应用。

还没有尝试过多个属性,但应该没问题。

于 2015-03-25T05:30:35.567 回答