2

Using Rob Mayoff's answer to create GIF, I've adapted his code to the following in Swift.

let kFrameCount:Int = 6

let frames:NSNumber = NSNumber(float: 20000.0) //No matter what number I place here...GIF runs at same speed

let fileProperties = [kCGImagePropertyGIFLoopCount as String: 0]
let frameProperties = [kCGImagePropertyGIFDelayTime as String: frames]

let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as! NSURL
let fileURL = documentsUrl.URLByAppendingPathComponent("animated.gif")

var destination: CGImageDestination = CGImageDestinationCreateWithURL(fileURL, kUTTypeGIF, kFrameCount, nil)
var destination2: Void = CGImageDestinationSetProperties(destination, fileProperties)

for i in 0..<kFrameCount {
   CGImageDestinationAddImage(destination, imagessss[i].CGImage, frameProperties)
}

if (!CGImageDestinationFinalize(destination)) {
        println("fail")
}

println(fileURL)

let activityVC: UIActivityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)

This creates the GIF. But no matter what amount I place as the float, 20000.0 or 0.01, the GIF runs at the same DelayTime. Also if I change the value in fileProperties from 0 to any other number, the GIF still does a continuous loop.

I feel the error lies in the line

var destination2: Void = CGImageDestinationSetProperties(destination, fileProperties)

or my declaration of fileProperties and frameProperties

Does Anyone know why my fileProperties and frameProperties are not effecting the GIF?

P.S. I can effectively slowdown the gif by increasing the frames and lacing the imagessss array with duplicate images, But this isn't ideal.

Community
  • 1
  • 1
Chameleon
  • 1,608
  • 3
  • 21
  • 39
  • Interesting piece of code about the delay problem: http://stackoverflow.com/a/17824564/2227743 – Eric Aya May 27 '15 at 12:03
  • shouldn't the delayTime be an innate property of the GIF file, decided at writing? – Chameleon May 27 '15 at 12:36
  • Look at this example: https://gist.github.com/mayoff/4969104 search for kCGImagePropertyGIFDelayTime – Matic Oblak May 27 '15 at 13:28
  • @MaticOblak this is the same code as the link i provided. that I adapted the Swift version from – Chameleon May 27 '15 at 13:45
  • Oh, great then. But you wrote it wrong. You are missing the other constant which wraps the value you are setting. Check again.. – Matic Oblak May 27 '15 at 13:47
  • yes that is very possible, I don't know ObjC and Swift is my only, and still very new, language. if you could point me to which line? i spent 14 hours yesterday just to produce this. – Chameleon May 27 '15 at 19:21
  • @EricD. So are you saying that my GIF might be writing with the correct delayTime but depending on what 'program' it is expressed 'on', the frame duration might be expressed at a default rate? – Chameleon Jun 01 '15 at 08:38
  • Oh *I* am not saying anything. ;) But the answer I mentioned does, and indeed I believe it's true, apps can change the delay/rate of a GIF. I have no expertise on this, though. – Eric Aya Jun 01 '15 at 08:40

1 Answers1

3

You are missing the kCGImagePropertyGIFDictionary level in your properties.

Change you properties to:

let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frames]]

(I'm using Xcode 6.4)

Lluis Gerard
  • 1,623
  • 17
  • 16