我正在尝试转换捕获的视频并将其转换为Gif,但我在保存已转换的Gif并将其保存到UIImage对象时遇到了问题。 Ultimatley,我正在尝试使用此对象在UIImageView上显示gif。另外,请注意我正在使用NSGIF将我的视频转换为Gifs。这是我的代码:
@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 :(得分:0)
首先,您要声明一个不需要的常量let gifURL: NSURL
。除了事实它是一个常数而不是变量,因此无论如何都不能设置它。它与闭包中的gifURL
不同。这是第二个定义,无论如何都会覆盖第一个。删除let
行。
其次,我不知道NSGIF库,但你的逻辑是倒退的。在致电NSGIF.createGIFfromURL
创建该文件之前,您正在使用参数outputFileURL
调用videoFileOutput?.startRecordingToOutputFileURL
来指示要转换的文件。
我认为你想先录制,然后当你停止录制时,转换文件。
因此,请尝试将createGIFfromURL
调用移至行else
后的videoFileOutput?.stopRecording()
块。
我担心主线上会发生这种情况。 videoFileOutput
对象似乎有delegate
。录制完成后会收到活动吗?如果是这样,那将是转换文件的更好地方。