Skip to content

Commit

Permalink
Merge pull request AnyImageKit#187 from AnyImageKit/fix/preferredOutp…
Browse files Browse the repository at this point in the history
…utPath

Fix preferred output path
  • Loading branch information
anotheren authored Jan 5, 2024
2 parents 62ccb21 + a841f9e commit f531629
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ extension PadCaptureViewController {

private func convertMovToMp4(_ url: URL, completion: @escaping ((URL?) -> Void)) {
let avAsset = AVURLAsset(url: url, options: nil)
let outputURL = FileHelper.getTemporaryUrl(by: .video, fileType: .mp4)
let outputURL = FileHelper.getTemporaryURL(by: .video, fileType: .mp4)

let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetHighestQuality)!
exportSession.outputURL = outputURL
Expand Down
2 changes: 1 addition & 1 deletion Sources/AnyImageKit/Capture/Util/Recorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extension Recorder {

private func createWriter() -> AVAssetWriter? {
do {
let outputURL = FileHelper.getTemporaryUrl(by: .video, fileType: .mp4)
let outputURL = FileHelper.getTemporaryURL(by: .video, fileType: .mp4)
_print("Create AVAssetWriter at utl: \(outputURL)")
return try AVAssetWriter(outputURL: outputURL, fileType: .mp4)
} catch {
Expand Down
28 changes: 20 additions & 8 deletions Sources/AnyImageKit/Core/Util/Common/FileHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ struct FileHelper {
extension FileHelper {

@discardableResult
static func write(photoData: Data, fileType: FileType, filename: String = "") -> URL? {
write(photoData: photoData, utType: fileType.utType, filename: filename)
static func write(photoData: Data, fileType: FileType, dir: String = "", filename: String = "") -> URL? {
write(photoData: photoData, utType: fileType.utType, dir: dir, filename: filename)
}

@discardableResult
static func write(photoData: Data, utType: CFString, filename: String = "") -> URL? {
let url = getTemporaryUrl(by: .photo, utType: utType, filename: filename)
static func write(photoData: Data, utType: CFString, dir: String = "", filename: String = "") -> URL? {
let url = getOutputURL(by: .photo, utType: utType, dir: dir, filename: filename)
do {
try photoData.write(to: url)
return url
Expand All @@ -61,7 +61,7 @@ extension FileHelper {
}

static func read(utType: CFString, filename: String) -> Data? {
let url = getTemporaryUrl(by: .photo, utType: utType, filename: filename)
let url = getTemporaryURL(by: .photo, utType: utType, filename: filename)
if !FileManager.default.fileExists(atPath: url.path) { return nil }
do {
return try Data(contentsOf: url)
Expand Down Expand Up @@ -89,15 +89,27 @@ extension FileHelper {
}
}

static func getTemporaryUrl(by type: MediaType, fileType: FileType, filename: String = "") -> URL {
return getTemporaryUrl(by: type, utType: fileType.utType, filename: filename)
static func getTemporaryURL(by type: MediaType, fileType: FileType, filename: String = "") -> URL {
return getTemporaryURL(by: type, utType: fileType.utType, filename: filename)
}

static func getTemporaryUrl(by type: MediaType, utType: CFString, filename: String = "") -> URL {
static func getTemporaryURL(by type: MediaType, utType: CFString, filename: String = "") -> URL {
let tmpPath = temporaryDirectory(for: type)
let name = filename.isEmpty ? dateString() : filename
let filePath = tmpPath.appending("\(name).\(fileExtension(from: utType))")
createDirectory(at: tmpPath)
return URL(fileURLWithPath: filePath)
}

static func getOutputURL(by type: MediaType, utType: CFString, dir: String = "", filename: String = "") -> URL {
if dir.isEmpty {
return getTemporaryURL(by: .photo, utType: utType, filename: filename)
} else {
let dirURL = URL(fileURLWithPath: dir)
let name = filename.isEmpty ? dateString() : filename
let fileURL = dirURL.appendingPathComponent("\(name).\(fileExtension(from: utType))")
createDirectory(at: dir)
return fileURL
}
}
}
20 changes: 13 additions & 7 deletions Sources/AnyImageKit/Core/Util/Common/ScreenHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@ struct ScreenHelper {
if #available(iOS 13.0, *) {
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, let statusBarFrame = windowScene.statusBarManager?.statusBarFrame {
return statusBarFrame
} else {
return .zero
}
} else {
return UIApplication.shared.statusBarFrame
}
return UIApplication.shared.statusBarFrame
}

static var mainBounds: CGRect {
if #available(iOS 13.0, *) {
for scene in UIApplication.shared.connectedScenes {
if scene.activationState == .foregroundActive, let delegate = scene.delegate as? UIWindowSceneDelegate, let bounds = delegate.window??.bounds {
return bounds
}
}
let keyWindow = UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.map { $0 as? UIWindowScene }
.compactMap { $0 }
.first?.windows
.filter { $0.isKeyWindow }.first
return keyWindow?.bounds ?? .zero
} else {
return UIApplication.shared.windows[0].bounds
}
return UIApplication.shared.windows[0].bounds
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension ExportTool {
return ExportTool.requestPhotoData(for: asset, options: photoDataOptions) { result, requestID in
switch result {
case .success(let response):
guard let outputURL = FileHelper.write(photoData: response.data, utType: response.dataUTI as CFString) else {
guard let outputURL = FileHelper.write(photoData: response.data, utType: response.dataUTI as CFString, dir: options.preferredOutputPath) else {
completion(.failure(.fileWriteFailed), requestID)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ extension ExportTool {
}
// Prepare Output URL
let dateString = FileHelper.dateString()
let outputPath = options.preferredOutputPath.appending("VIDEO-\(dateString).mp4")
let outputURL = URL(fileURLWithPath: outputPath)
let outputDir = URL(fileURLWithPath: options.preferredOutputPath)
let outputURL = outputDir.appendingPathComponent("VIDEO-\(dateString).mp4")
// Setup Export Session
exportSession.shouldOptimizeForNetworkUse = true
exportSession.outputFileType = .mp4
Expand Down

0 comments on commit f531629

Please sign in to comment.