我正在使用“录制”按钮,用户可以按住该按钮来录制他们的视频。当用户停止按住“录制”按钮时,我想将他们录制的视频保存到相册。我对UISaveVideoAtPathToSavedPhotosAlbum函数有点困惑。到目前为止我的代码:
@IBAction func RecordAction(sender: UIButton) {
// User is holding down record button
if (!isRecording) {
do {
self.isRecording = true
let documentsDir = try NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain:.UserDomainMask, appropriateForURL:nil, create:true)
let fileURL = NSURL(string:"test.mp4", relativeToURL:documentsDir)!
do {
try NSFileManager.defaultManager().removeItemAtURL(fileURL)
} catch {
}
movieOutput = try MovieOutput(URL:fileURL, size:Size(width:400, height:645), liveVideo:true)
camera.audioEncodingTarget = movieOutput
filter --> movieOutput!
movieOutput!.startRecording()
} catch {
fatalError("Couldn't initialize movie, error: \(error)")
}
}
}
@IBAction func RecordDone(sender: UIButton) {
// After user stops holding down the button, save video to gallery
if isRecording == true {
movieOutput?.finishRecording {
self.isRecording = false
self.camera.audioEncodingTarget = nil
self.movieOutput = nil
UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie, nil, nil, nil)
}
}
}
非常感谢任何帮助。
答案 0 :(得分:0)
尝试使用此功能保存视频:
func StartWritingvideo()
{
player.pause()
filters.removeAllObjects()
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let filePath : String!
if trim == true
{
filePath = paths[0].stringByAppendingString("/output/outPut.mov")
}
else
{
filePath = paths[0].stringByAppendingString("/image.mov")
}
self.movieFile = GPUImageMovie(URL: NSURL(fileURLWithPath: filePath))
self.movieFile.runBenchmark = true
self.movieFile.playAtActualSpeed = false
filters = [GPUImageAmatorkaFilter(),GPUImageMissEtikateFilter(),GPUImageSoftEleganceFilter(),FIIIN()]
let indexpaths = self.collectionView!.indexPathsForSelectedItems()!
let indexpath = indexpaths[0] as NSIndexPath
self.movieFile.addTarget(self.filters[indexpath.row] as! GPUImageInput)
let pathToMovie = paths[0].stringByAppendingPathComponent("IMG_filtered.MOV")
do
{
try NSFileManager.defaultManager().removeItemAtPath(pathToMovie)
}
catch
{
error as NSError
}
let Data = NSData(contentsOfURL: NSURL(fileURLWithPath: filePath))
print( Data?.length )
let anAsset = AVAsset(URL: NSURL(fileURLWithPath: filePath))
let videoAssetTrack = anAsset.tracksWithMediaType(AVMediaTypeVideo)[0]
var naturalSize = CGSize()
print(naturalSize)
naturalSize = videoAssetTrack.naturalSize
self.movieWriter = GPUImageMovieWriter(movieURL: NSURL(fileURLWithPath:pathToMovie), size: naturalSize)
let input = self.filters[indexpath.row] as! GPUImageOutput
input.addTarget(self.movieWriter)
self.movieWriter.shouldPassthroughAudio = true
if anAsset.tracksWithMediaType(AVMediaTypeAudio).count > 0 {
self.movieFile.audioEncodingTarget = self.movieWriter
}
else
{
self.movieFile.audioEncodingTarget = nil
}
self.movieFile.enableSynchronizedEncodingUsingMovieWriter(self.movieWriter)
self.movieWriter.startRecording()
self.movieFile.startProcessing()
self.movieWriter.completionBlock =
{() -> Void in
self.stop = false
self.movieWriter.finishRecording()
self.movieFile.cancelProcessing()
input.removeAllTargets()
self.movieFile.removeAllTargets()
if self.isLive == false
{
ALAssetsLibrary().writeVideoAtPathToSavedPhotosAlbum(NSURL(fileURLWithPath: pathToMovie), completionBlock:
{_ in
self.dismissViewControllerAnimated(false, completion: nil)
self.player.pause()
})
}else {
self.dismissViewControllerAnimated(false, completion: nil)
}
}
}
我对GPUImageFilter有很好的了解如果你需要任何帮助,请问我。如果您觉得我的回答有帮助,请放弃标记。