Swift: создание GIF с анимацией при переходе изображения - PullRequest
1 голос
/ 11 апреля 2019

Я хочу создать GIF из массива изображений, который при переходе изображения должен постепенно исчезать (Анимация)

Я создал функцию, которая создает GIF из изображений, но без анимации.

Ниже мой код:

var urls : URL? = nil

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

let documentsDirectoryURL: URL? = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL: URL? = documentsDirectoryURL?.appendingPathComponent("animated.gif")

if let url = fileURL as CFURL? {

    if let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, myImages.count, nil) {

        CGImageDestinationSetProperties(destination, fileProperties)

        for image in myImages {
            if let cgImage = image.cgImage {
                CGImageDestinationAddImage(destination, cgImage, frameProperties)
            }
        }

        if !CGImageDestinationFinalize(destination) {
            print("Failed to finalize the image destination")
        }

        if let url = fileURL {
            urls = url
        } else {
            print("No file url found!")
        }
    }
}

return (urls ?? nil)!
...