     }
     
     // GIF conversion methods
-    func convertVideoToGif(videoURL: NSURL) {
-        let regift = Regift(sourceFileURL: videoURL as URL, frameCount: frameCount, delayTime: delayTime, loopCount: loopCount)
+    func convertVideoToGif(videoURL: NSURL, start: NSNumber?, duration: NSNumber?) {
+        DispatchQueue.main.async {
+            self.dismiss(animated: true, completion: nil)
+        }
+        
+        var regift: Regift
+        
+        if let start = start {
+            // Trimmed
+            regift = Regift(sourceFileURL: videoURL as URL, destinationFileURL: nil, startTime: Float(start), duration: Float(duration!), frameRate: frameRate, loopCount: loopCount)
+        } else {
+            // Untrimmed
+            regift = Regift(sourceFileURL: videoURL as URL, destinationFileURL: nil, frameCount: frameCount, delayTime: delayTime, loopCount: loopCount)
+        }
+        
         let gifURL = regift.createGif()
         let gif = Gif(url: gifURL! as NSURL, videoURL: videoURL, caption: nil)
         

        if mediaType == kUTTypeMovie as String {
            let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
            //UISaveVideoAtPathToSavedPhotosAlbum(videoURL.path!, nil, nil, nil)
            dismiss(animated: true, completion: nil)
            convertVideoToGif(videoURL: videoURL)
        }
    }


import Foundation
import UIKit
import MobileCoreServices
import AVFoundation

// Regift constants
let frameCount = 16
let frameRate = 15
let delayTime: Float = 0.2
let loopCount = 0  // 0 means loop forever

