gpt4 book ai didi

ios - swift 3 : how to save Animated images from imageView (GIF)?

转载 作者:可可西里 更新时间:2023-11-01 01:26:19 24 4
gpt4 key购买 nike

我是 swift 的新手,我有一些 images 在一个 imageView 中动画,现在我想把它保存在画廊中,因为我们保存简单的图像,我该怎么做?

imageView.animationImages = [
UIImage(named:"1.jpg")!,
UIImage(named:"2.jpg")!,
UIImage(named:"3.jpg")!,
UIImage(named:"4.jpg")!,
UIImage(named:"5.jpg")!
]

imageView.animationDuration = 3
imageView.startAnimating()

我们将不胜感激...(:

最佳答案

请引用这里的代码 -> swift-create-a-gif-from-images-and-turn-it-into-nsdata

上面的代码是针对OS X的,所以稍微调整了一下。

导入 ImageIOMobileCoreServices

func createGIF(with images: [UIImage], name: URL, loopCount: Int = 0, frameDelay: Double) {

let destinationURL = name
let destinationGIF = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypeGIF, images.count, nil)!

// 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): frameDelay]
]


for img in images {
// Convert an UIImage to CGImage, fitting within the specified rect
let cgImage = img.cgImage
// Add the frame to the GIF image
CGImageDestinationAddImage(destinationGIF, cgImage!, properties as CFDictionary?)
}

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

这样使用:

let documentsURL = FileManager.default.urls(for: .documentDirectory,
in: .userDomainMask).first
let path = documentsURL!.appendingPathComponent("1.gif")
createGIF(with: images, name: path, frameDelay: 0.1)
// you can check the path and open it in Finder, then you can see the file
print(path)

我也在github上做了一个demo -> saveImagesAsGif

关于ios - swift 3 : how to save Animated images from imageView (GIF)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41437419/

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