Swift:无法转换类型为';()->;()';关闭结果类型';无效';
这是所有的密码。有一个按钮按下操作函数调用prepareVideoSwift:无法转换类型为';()->;()';关闭结果类型';无效';,swift,Swift,这是所有的密码。有一个按钮按下操作函数调用prepareVideo @IBAction func nextButtonPressed(_ sender: Any) { if MyVariables.isScreenshot == true { prepareScreenshot { self.moveOn() } } else { prepareVideo() } } func prepareV
@IBAction func nextButtonPressed(_ sender: Any) {
if MyVariables.isScreenshot == true {
prepareScreenshot {
self.moveOn()
}
} else {
prepareVideo()
}
}
func prepareVideo(){
let outputFileName = NSUUID().uuidString
let outputFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)
self.videoURL = URL(fileURLWithPath: outputFilePath)
trimVideo(sourceURL: self.footageURL!, destinationURL: self.videoURL!, trimPoints: [(trimmerView.startTime!,trimmerView.endTime!)], completion: prepareVideoThumbnail {
self.moveOn
}) //Xcode mentions error here
}
func trimVideo (sourceURL: URL, destinationURL: URL, trimPoints: TrimPoints, completion: @escaping () -> ()) {
guard sourceURL.isFileURL else { return }
guard destinationURL.isFileURL else { return }
let options = [
AVURLAssetPreferPreciseDurationAndTimingKey: true
]
let asset = AVURLAsset(url: sourceURL, options: options)
let preferredPreset = AVAssetExportPresetPassthrough
if verifyPresetForAsset(preset: preferredPreset, asset: asset) {
let composition = AVMutableComposition()
let videoCompTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: CMPersistentTrackID())
let audioCompTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: CMPersistentTrackID())
guard let assetVideoTrack: AVAssetTrack = asset.tracks(withMediaType: .video).first else { return }
guard let assetAudioTrack: AVAssetTrack = asset.tracks(withMediaType: .audio).first else { return }
var accumulatedTime = kCMTimeZero
for (startTimeForCurrentSlice, endTimeForCurrentSlice) in trimPoints {
let durationOfCurrentSlice = CMTimeSubtract(endTimeForCurrentSlice, startTimeForCurrentSlice)
let timeRangeForCurrentSlice = CMTimeRangeMake(startTimeForCurrentSlice, durationOfCurrentSlice)
do {
try videoCompTrack!.insertTimeRange(timeRangeForCurrentSlice, of: assetVideoTrack, at: accumulatedTime)
try audioCompTrack!.insertTimeRange(timeRangeForCurrentSlice, of: assetAudioTrack, at: accumulatedTime)
accumulatedTime = CMTimeAdd(accumulatedTime, durationOfCurrentSlice)
}
catch let compError {
print("TrimVideo: error during composition: \(compError)")
}
}
guard let exportSession = AVAssetExportSession(asset: composition, presetName: preferredPreset) else { return }
exportSession.outputURL = destinationURL as URL
exportSession.outputFileType = AVFileType.m4v
exportSession.shouldOptimizeForNetworkUse = true
removeFileAtURLIfExists(url: destinationURL as URL)
exportSession.exportAsynchronously {
completion()
}
}
else {
print("TrimVideo - Could not find a suitable export preset for the input video")
}
}
func prepareVideoThumbnail(completion: @escaping () -> Void) {
guard let VideoURL = self.videoURL else { return }
self.thumbnailImage = setThumbnailFrom(path: VideoURL)
completion()
//moveOn()
//DispatchQueue.main.async {
// self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
//}
}
func moveOn(){
guard self.thumbnailImage != nil else {
return
}
if MyVariables.isScreenshot == true {
guard self.screenshotOut != nil else {
return
}
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
} else {
guard self.thumbnailImage != nil else {
return
}
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
//now I set those three varibles?
}
}
PrepareVideo不接受回调/完成处理程序,但它调用trimVideo,trimVideo接受完成处理程序。在这里,我调用prepareVideoThumbnail,它应该调用它的完成处理程序{
自我移动
}.PrepareVideo缩略图应为trimVideo怀疑的类型()->()。所以我不知道为什么它会抱怨闭包结果类型“Void”。我知道self.moveOn()会导致这种情况,但我没有调用它,注意我使用的self.moveOn没有括号。我该如何解决这个问题 的语法:
trimVideo(sourceURL: self.footageURL!, destinationURL: self.videoURL!, trimPoints: [(trimmerView.startTime!,trimmerView.endTime!)], completion: prepareVideoThumbnail {
self.moveOn
})
应该是:
trimVideo(sourceURL: self.footageURL!, destinationURL: self.videoURL!, trimPoints: [(trimmerView.startTime!,trimmerView.endTime!)]) {
self.prepareVideoThumbnail() {
self.moveOn()
}
}
顺便说一句,对于不进行任何异步处理的函数(如preparrevideombuilt
函数),使用completion参数是没有意义的
然后您的代码变成:
trimVideo(sourceURL: self.footageURL!, destinationURL: self.videoURL!, trimPoints: [(trimmerView.startTime!,trimmerView.endTime!)]) {
self.prepareVideoThumbnail()
self.moveOn()
}
并消除
prepareVideoThumbnail
函数上的完成参数。ah ok。那么他们应该直接调用下一个函数吗?