2024-08-17 13:54:54 +00:00
|
|
|
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 {
|
2024-08-18 15:56:47 +00:00
|
|
|
continuation.resume(throwing: AppError.imageNotFound)
|
2024-08-17 13:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-08-18 15:56:47 +00:00
|
|
|
continuation.resume(throwing: AppError.videoNotFound)
|
2024-08-17 13:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|