Skip to content

Commit

Permalink
Merge pull request #3 from rubygarage/feature/file_check_completion_fix
Browse files Browse the repository at this point in the history
Added temporary file check before deleting
  • Loading branch information
jowkame authored Aug 11, 2017
2 parents f7d7749 + 56724ae commit 0895ed4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Example/MediaWatermark/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavig
resultImageView.addSubview(videoPlayer.view)

if let item = MediaItem(url: url) {
let logoImage = UIImage(named: "rglogo")
let logoImage = UIImage(named: "logo")

let firstElement = MediaElement(image: logoImage!)
firstElement.frame = CGRect(x: 0, y: 0, width: logoImage!.size.width, height: logoImage!.size.height)
Expand Down
48 changes: 29 additions & 19 deletions MediaWatermark/Classes/Core/MediaVideoProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ extension MediaProcessor {
try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, item.sourceAsset.duration), of: clipVideoTrack!, at: kCMTimeZero)
} catch {
completion(MediaProcessResult(processedUrl: nil, image: nil), error)
return
}

do {
try compositionAudioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, item.sourceAsset.duration), of: clipAudioTrack!, at: kCMTimeZero)
} catch {
completion(MediaProcessResult(processedUrl: nil, image: nil), error)
return
}

compositionVideoTrack.preferredTransform = (item.sourceAsset.tracks(withMediaType: AVMediaTypeVideo).first?.preferredTransform)!
Expand Down Expand Up @@ -66,20 +68,24 @@ extension MediaProcessor {
videoComposition.instructions = [instruction]

let processedUrl = processedMoviePath()
clearTemporaryData(url: processedUrl, completion: completion)

let exportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
exportSession?.videoComposition = videoComposition
exportSession?.outputURL = processedUrl
exportSession?.outputFileType = AVFileTypeQuickTimeMovie

exportSession?.exportAsynchronously(completionHandler: {
if exportSession?.status == AVAssetExportSessionStatus.completed {
completion(MediaProcessResult(processedUrl: processedUrl, image: nil), nil)
} else {
completion(MediaProcessResult(processedUrl: nil, image: nil), exportSession?.error)
}
})
let result = clearTemporaryData(url: processedUrl)

if result.success {
let exportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
exportSession?.videoComposition = videoComposition
exportSession?.outputURL = processedUrl
exportSession?.outputFileType = AVFileTypeQuickTimeMovie

exportSession?.exportAsynchronously(completionHandler: {
if exportSession?.status == AVAssetExportSessionStatus.completed {
completion(MediaProcessResult(processedUrl: processedUrl, image: nil), nil)
} else {
completion(MediaProcessResult(processedUrl: nil, image: nil), exportSession?.error)
}
})
} else {
completion(MediaProcessResult(processedUrl: nil, image: nil), result.error)
}
}

// MARK: - private
Expand Down Expand Up @@ -108,12 +114,16 @@ extension MediaProcessor {
return URL(fileURLWithPath: documentsPath)
}

private func clearTemporaryData(url: URL, completion: ProcessCompletionHandler!) {
do {
try FileManager.default.removeItem(at: url)
} catch {
completion(MediaProcessResult(processedUrl: nil, image: nil), error)
private func clearTemporaryData(url: URL) -> (success: Bool, error: Error?) {
if (FileManager.default.fileExists(atPath: url.path)) {
do {
try FileManager.default.removeItem(at: url)
return (true, nil)
} catch {
return (false, error)
}
}
return (true, nil)
}

private func resolutionSizeForLocalVideo(url: URL) -> CGSize {
Expand Down

0 comments on commit 0895ed4

Please sign in to comment.