         dismiss(animated: true, completion: nil)
     }
     
-    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( execute: {
+            self.dismiss(animated: true, completion: nil)
+        })
+        var regift: Regift!
+        if let _ = start {
+            // trimmed gif
+            regift = Regift(sourceFileURL: videoURL as URL, destinationFileURL: nil, startTime: start as! Float, duration: duration as! Float, frameRate: frameCount, loopCount: loopCount)
+        } else {
+            // untrimmed gif
+            regift = Regift(sourceFileURL: videoURL as URL, frameCount: frameCount, delayTime: delayTime, loopCount: loopCount)
+        }
         let gifURL = regift.createGif()
         let gif = Gif(url: gifURL!, videoURL: videoURL as URL, caption: nil)
         displayGIF(gif: gif)

        let mediaType = info[UIImagePickerControllerMediaType] as! String
        if mediaType == kUTTypeMovie as String {
            let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
            dismiss(animated: true, completion: nil)
            convertVideoToGIF(videoURL: videoURL)
        }
    }
    

        let videoPicker = UIImagePickerController()
        videoPicker.sourceType = .photoLibrary
        videoPicker.mediaTypes = [kUTTypeMovie as String]
        videoPicker.delegate = self
        videoPicker.allowsEditing = true
        videoPicker.videoQuality = .typeHigh
        self.present(videoPicker, animated: true, completion: nil)
    }
