2016-05-17 73 views
0

我想转换捕获的视频并将其转换成Gif,但我在保存转换后的Gif并将其保存到UIImage对象时遇到了问题。 Ultimatley,我试图用这个对象在UIImageView上显示gif。另外,请注意,我使用NSGIF将我的视频转换为GIF。这是我的代码:保存gif到UIImage

@IBOutlet weak var tempImageView3: UIImageView! 

@IBAction func capture(sender: AnyObject) { 
    if !isRecording { 
     isRecording = true 

     UIView.animateWithDuration(0.5, delay: 0.0, options: [.Repeat, .Autoreverse, .AllowUserInteraction], animations: {() ->() in 
      self.cameraButton.transform = CGAffineTransformMakeScale(0.5, 0.5) 
      }, completion: nil) 

     let outputPath = NSTemporaryDirectory() + "output.mov" 
     let outputFileURL = NSURL(fileURLWithPath: outputPath) 
     let gifURL: NSURL 

     NSGIF.createGIFfromURL(outputFileURL, withFrameCount: 16, delayTime: Float(0.2), loopCount: 0, completion: { (gifURL) -> Void in 
      print("output saved \(gifURL)") 
     }) 

     videoFileOutput?.startRecordingToOutputFileURL(outputFileURL, recordingDelegate: self) 
    } else { 
     isRecording = false 
     UIView.animateWithDuration(0.5, delay: 1.0, options: [], animations: {() ->() in 
      self.cameraButton.transform = CGAffineTransformMakeScale(1.0, 1.0) 
      }, completion: nil) 
     cameraButton.layer.removeAllAnimations() 
     videoFileOutput?.stopRecording() 

    } 
} 

回答

0

首先,你声明一个不需要的常量let gifURL: NSURL。除了这个事实,它是一个常数而不是一个变量,所以无论如何都没有价值。这与闭包中的gifURL不一样。这是第二个定义,无论如何都会覆盖第一个定义。删除let行。

其次,我不知道NSGIF库,但你的逻辑出现倒退。在调用videoFileOutput?.startRecordingToOutputFileURL来创建该文件之前,您使用参数outputFileURL指定要转换的文件,并调用NSGIF.createGIFfromURL

我想你会想先做录音,然后当你停止录音时,转换文件。

因此,请尝试将createGIFfromURL调用移动到线路videoFileOutput?.stopRecording()后面的else块。

虽然我一直在担心发生在主线程上。 videoFileOutput对象似乎有一个delegate。记录完成后是否收到事件?如果是这样,那将是一个更好的地方来转换文件。