Skip to content

Commit 9e20a46

Browse files
authored
Merge pull request #492 from mpretty-cyro/fix/video-attachment-quality
Fixed an issue with video attachment quality
2 parents a578a0e + 53199d4 commit 9e20a46

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Session/Media Viewing & Editing/PhotoLibrary.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,34 @@ class PhotoCollectionContents {
238238
Future { [weak self] resolver in
239239
let options: PHVideoRequestOptions = PHVideoRequestOptions()
240240
options.isNetworkAccessAllowed = true
241+
options.deliveryMode = .highQualityFormat
241242

242-
_ = self?.imageManager.requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetMediumQuality) { exportSession, info in
243+
self?.imageManager.requestAVAsset(forVideo: asset, options: options) { avAsset, _, info in
243244

244245
if let error: Error = info?[PHImageErrorKey] as? Error {
245246
return resolver(.failure(error))
246247
}
247248

248-
guard let exportSession = exportSession else {
249+
guard let avAsset: AVAsset = avAsset else {
250+
return resolver(Result.failure(PhotoLibraryError.assertionError(description: "avAsset was unexpectedly nil")))
251+
}
252+
253+
let compatiblePresets = AVAssetExportSession.exportPresets(compatibleWith: avAsset)
254+
var bestExportPreset: String
255+
256+
if compatiblePresets.contains(AVAssetExportPresetPassthrough) {
257+
bestExportPreset = AVAssetExportPresetPassthrough
258+
Log.debug("[PhotoLibrary] Using Passthrough export preset.")
259+
} else {
260+
bestExportPreset = AVAssetExportPresetHighestQuality
261+
Log.debug("[PhotoLibrary] Passthrough not available. Falling back to HighestQuality export preset.")
262+
}
263+
264+
if (info?[PHImageCancelledKey] as? Bool) == true {
265+
return resolver(.failure(PhotoLibraryError.assertionError(description: "Video request cancelled")))
266+
}
267+
268+
guard let exportSession: AVAssetExportSession = AVAssetExportSession(asset: avAsset, presetName: bestExportPreset) else {
249269
resolver(Result.failure(PhotoLibraryError.assertionError(description: "exportSession was unexpectedly nil")))
250270
return
251271
}

0 commit comments

Comments
 (0)