Skip to content

Commit 259b850

Browse files
committed
Fixed an issue with video attachment quality
1 parent ee085c6 commit 259b850

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Session/Media Viewing & Editing/PhotoLibrary.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,34 @@ class PhotoCollectionContents {
174174
Future { [weak self] resolver in
175175
let options: PHVideoRequestOptions = PHVideoRequestOptions()
176176
options.isNetworkAccessAllowed = true
177+
options.deliveryMode = .highQualityFormat
177178

178-
_ = self?.imageManager.requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetMediumQuality) { exportSession, foo in
179+
self?.imageManager.requestAVAsset(forVideo: asset, options: options) { avAsset, _, info in
179180

180-
guard let exportSession = exportSession else {
181+
if let error: Error = info?[PHImageErrorKey] as? Error {
182+
return resolver(.failure(error))
183+
}
184+
185+
guard let avAsset: AVAsset = avAsset else {
186+
return resolver(Result.failure(PhotoLibraryError.assertionError(description: "avAsset was unexpectedly nil")))
187+
}
188+
189+
let compatiblePresets = AVAssetExportSession.exportPresets(compatibleWith: avAsset)
190+
var bestExportPreset: String
191+
192+
if compatiblePresets.contains(AVAssetExportPresetPassthrough) {
193+
bestExportPreset = AVAssetExportPresetPassthrough
194+
Log.debug("[PhotoLibrary] Using Passthrough export preset.")
195+
} else {
196+
bestExportPreset = AVAssetExportPresetHighestQuality
197+
Log.debug("[PhotoLibrary] Passthrough not available. Falling back to HighestQuality export preset.")
198+
}
199+
200+
if (info?[PHImageCancelledKey] as? Bool) == true {
201+
return resolver(.failure(PhotoLibraryError.assertionError(description: "Video request cancelled")))
202+
}
203+
204+
guard let exportSession: AVAssetExportSession = AVAssetExportSession(asset: avAsset, presetName: bestExportPreset) else {
181205
resolver(Result.failure(PhotoLibraryError.assertionError(description: "exportSession was unexpectedly nil")))
182206
return
183207
}

0 commit comments

Comments
 (0)