den Code finden Sie hier. ->swift-create-a-gif-from-images-and-turn-it-into-nsdata
Der obige Code wird für OS X verwendet, also habe ich ihn ein wenig angepasst.
Import ImageIO
und MobileCoreServices
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)
}
es auf diese Weise verwenden:
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)
Ich habe auch eine Demo auf Github ->saveImagesAsGif
Mögliche Duplikat von [ein Bild zu Fotos Speichern Bibliothek mit Swift 2.0] (http://stackoverflow.com/questions/35761247/saving-an-image-to-photos-library-using-swift-2-0) –
@A rtemNovichkov Ich möchte Bild als GIF speichern, dass animieren. bitte entfernen Sie das mögliche doppelte Flag davon –