Files
suixinkan_uikit/suixinkan/Common/PhotoLibrarySaver.swift

38 lines
1.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// PhotoLibrarySaver.swift
// suixinkan
//
import Photos
import UIKit
/// 使
enum PhotoLibrarySaver {
///
static func saveImage(_ image: UIImage) async -> Bool {
let status = await requestAuthorizationIfNeeded()
guard status == .authorized || status == .limited else {
return false
}
return await withCheckedContinuation { continuation in
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image)
}, completionHandler: { success, _ in
continuation.resume(returning: success)
})
}
}
private static func requestAuthorizationIfNeeded() async -> PHAuthorizationStatus {
let current = PHPhotoLibrary.authorizationStatus(for: .addOnly)
switch current {
case .notDetermined:
return await PHPhotoLibrary.requestAuthorization(for: .addOnly)
default:
return current
}
}
}