gpt4 book ai didi

swift - 使用 Swift (macOS) 从图像制作 GIF

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:37 24 4
gpt4 key购买 nike

我想知道是否有办法在 macOS/osx 中使用 Swift 转换 NSImage 数组?

之后我应该能够将它导出到文件中,所以在我的应用程序上显示的图像动画是不够的。

谢谢!

最佳答案

图像 I/O 具有您需要的功能。试试这个:

var images = ... // init your array of NSImage

let destinationURL = NSURL(fileURLWithPath: "/path/to/image.gif")
let destinationGIF = CGImageDestinationCreateWithURL(destinationURL, kUTTypeGIF, images.count, nil)!

// The final size of your GIF. This is an optional parameter
var rect = NSMakeRect(0, 0, 350, 250)

// This dictionary controls the delay between frames
// If you don't specify this, CGImage will apply a default delay
let properties = [
(kCGImagePropertyGIFDictionary as String): [(kCGImagePropertyGIFDelayTime as String): 1.0/16.0]
]


for img in images {
// Convert an NSImage to CGImage, fitting within the specified rect
// You can replace `&rect` with nil
let cgImage = img.CGImageForProposedRect(&rect, context: nil, hints: nil)!

// Add the frame to the GIF image
// You can replace `properties` with nil
CGImageDestinationAddImage(destinationGIF, cgImage, properties)
}

// Write the GIF file to disk
CGImageDestinationFinalize(destinationGIF)

关于swift - 使用 Swift (macOS) 从图像制作 GIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38924365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com