What is wrong with my code?

i am trying to make a chat app but my code is not working someon please help, the build fails every time here is my code:

 

 

 

import UIKit

import ImageIO

 

extension UIImage {

 

    public class func gifWithData(_ data: Data) -> UIImage? {

        // Create source from data

        guard let source = CGImageSourceCreateWithData(Data, nil) else {

            print(“SwiftGif: Source for the image does not exist”)

            return nil

        }

 

        return UIImage.animatedImageWithSource(source)

    }

 

    public class func gifWithURL(_ gifUrl:String) -> UIImage? {

        // Validate URL

        guard let bundleURL:URL? = URL(string: gifUrl)

            else {

                print(“SwiftGif: This image named “(gifUrl)” does not exist”)

                return nil

        }

 

        // Validate data

        guard let imageData = try? Data(contentsOf: bundleURL!) else {

            print(“SwiftGif: Cannot turn image named “(gifUrl)” into NSData”)

            return nil

        }

 

        return gifWithData(imageData)

    }

 

    public class func gifWithName(_ name: String) -> UIImage? {

        // Check for existance of gif

        guard let bundleURL = Bundle.main

          .url(forResource: name, withExtension: “gif”) else {

            print(“SwiftGif: This image named “(name)” does not exist”)

            return nil

        }

 

        // Validate data

        guard let imageData = try? Data(contentsOf: bundleURL) else {

            print(“SwiftGif: Cannot turn image named “(name)” into NSData”)

            return nil

        }

 

        return gifWithData(imageData)

    }

 

    class func delayForImageAtIndex(_ index: Int, source: CGImageSource!) -> Double {

        var delay = 0.1

 

        // Get dictionaries

        let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil)

        let gifProperties: CFDictionary = unsafeBitCast(

            CFDictionaryGetValue(cfProperties,

                Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque()),

            to: CFDictionary.self)

 

        // Get delay time

        var delayObject: AnyObject = unsafeBitCast(

            CFDictionaryGetValue(gifProperties,

                Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque()),

            to: AnyObject.self)

        if delayObject.doubleValue == 0 {

            delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties,

                Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque()), to: AnyObject.self)

        }

 

        delay = delayObject as! Double

 

        if delay < 0.1 {

            delay = 0.1 // Make sure they’re not too fast

        }

 

        return delay

    }

 

    class func gcdForPair(_ a: Int?, _ b: Int?) -> Int {

        var a = a

        var b = b

        // Check if one of them is nil

        if b == nil || a == nil {

            if b != nil {

                return b!

            } else if a != nil {

                return a!

            } else {

                return 0

            }

        }

 

        // Swap for modulo

        if a< b{

            let c = a

            a = b

            b = c

        }

 

        // Get greatest common divisor

        var rest: Int

        while true {

            rest = a! % b!

 

            if rest == 0 {

                return b! // Found it

            } else {

                a = b

                b = rest

            }

        }

    }

 

    class func gcdForArray(_ array: Array) -> Int {

        if array.isEmpty {

            return 1

        }

 

        var gcd = array[0]

 

        for val in array {

            gcd = UIImage.gcdForPair(val, gcd)

        }

 

        return gcd

    }

 

    class func animatedImageWithSource(_ source: CGImageSource) -> UIImage? {

        let count = CGImageSourceGetCount(source)

        var images = [CGImage]()

        var delays = [Int]()

 

        // Fill arrays

        for i in 0..

            // Add image

            if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {

                images.append(image)

            }

 

            // At it’s delay in cs

            let delaySeconds = UIImage.delayForImageAtIndex(Int(i),

                source: source)

            delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms

        }

 

        // Calculate full duration

        let duration: Int = {

            var sum = 0

 

            for val: Int in delays {

                sum += val

            }

 

            return sum

            }()

 

        // Get frames

        let gcd = gcdForArray(delays)

        var frames = [UIImage]()

 

        var frame: UIImage

        var frameCount: Int

        for i in 0..

            frame = UIImage(cgImage: images[Int(i)])

            frameCount = Int(delays[Int(i)] / gcd)

 

            for _ in 0..

                frames.append(frame)

            }

        }

 

        // Heyhey

        let animation = UIImage.animatedImage(with: frames,

            duration: Double(duration) / 1000.0)

 

        return animation

    }

 

}

Powered by WPeMatico

About

You may also like...

Comments are closed.