Wednesday, January 31, 2024
HomeiOS Developmentios - The Body Delay for every picture shouldn't be working in...

ios – The Body Delay for every picture shouldn’t be working in Swift when making a GIF


Presently I’m engaged on a venture to create a GIF from some pictures. I used a technique (generateGifFromImages) from GitHub that takes a CGImage construction, which lets you set the body delay for every picture. However even with the body delay set to 2 seconds per body, the ensuing GIFs appear to play a lot sooner, sometimes round 0.5 seconds per body.

Under technique I used to transform pictures to gif.

public func generateGifFromImages(imagesArray: [CGImage], repeatCount: Int = 0, frameDelay: TimeInterval, targetSize: CGSize, destinationURL: URL, progressHandler: @escaping (Float) -> (), callback: @escaping (_ information: Knowledge?, _ error: NSError?) -> ()) {
    
    DispatchQueue.international(qos: .background).async {
        if let imageDestination = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypeGIF, imagesArray.rely, nil) {
            let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: repeatCount]]
            
            // Calculate complete progress steps
            let totalSteps = imagesArray.rely
            var currentStep = 0
            
            for picture in imagesArray {
                // Resize picture whereas sustaining facet ratio
                guard let resizedImage = resize(picture: picture, targetSize: targetSize) else {
                    callback(nil, self.errorFromString("Could not resize picture"))
                    return
                }
                
                let frameProperties: [String: Any] = [kCGImagePropertyGIFDelayTime as String: frameDelay]
                
                // Add the resized picture to the GIF
                CGImageDestinationAddImage(imageDestination, resizedImage, frameProperties as CFDictionary )
                
                // Replace progress
                currentStep += 1
                let progress = Float(currentStep) / Float(totalSteps)
                progressHandler(progress)
            }
            
            CGImageDestinationSetProperties(imageDestination, gifProperties as CFDictionary)
            
            if CGImageDestinationFinalize(imageDestination) {
                do {
                    let information = attempt Knowledge(contentsOf: destinationURL)
                    callback(information, nil)
                } catch {
                    callback(nil, error as NSError)
                }
            } else {
                callback(nil, self.errorFromString("Could not create the ultimate picture"))
            }
        }
    }
}

I attempted setting the body delay to 2 seconds for every picture utilizing the kCGImagePropertyGIFDelayTime property, however the ensuing GIF performs very quick. I’ve six pictures within the array, and I would like no less than a 1-second delay when every body is performed. Can somebody assist me please how you can regulate the body delay for every picture in order that the ensuing GIF performs in sluggish movement, displaying every body for no less than 1 second? Thanks upfront on your help!



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments