another.im-ios/AnotherIM/Helpers/PHImageManager+Fetch.swift

44 lines
1.4 KiB
Swift
Raw Permalink Normal View History

import Photos
import UIKit
extension PHImageManager {
func getPhoto(for asset: PHAsset) async throws -> UIImage {
let options = PHImageRequestOptions()
options.version = .original
options.isSynchronous = true
return try await withCheckedThrowingContinuation { continuation in
requestImage(
for: asset,
targetSize: PHImageManagerMaximumSize,
contentMode: .aspectFill,
options: options
) { image, _ in
if let image {
continuation.resume(returning: image)
} else {
continuation.resume(throwing: AppError.imageNotFound)
}
}
}
}
func getVideo(for asset: PHAsset) async throws -> AVAsset {
let options = PHVideoRequestOptions()
options.version = .original
options.deliveryMode = .highQualityFormat
options.isNetworkAccessAllowed = true
return try await withCheckedThrowingContinuation { continuation in
requestAVAsset(
forVideo: asset,
options: options
) { avAsset, _, _ in
if let avAsset {
continuation.resume(returning: avAsset)
} else {
continuation.resume(throwing: AppError.videoNotFound)
}
}
}
}
}