GBA001/Delta/Extensions/PHPhotoLibrary+Authorization.swift
Chris Rittenhouse 6bdc05f640
[Experimental Feature] Game Screenshots (#242)
* Adds extension to handle interactions with Photos app
- isAuthorized to check/ask for authorization
- saveUIImage to save a UIImage to Photos

* Adds a Pause Menu button for taking game screenshots

* Adds @Feature and @Options for Game Screenshots

* Implements Game Screenshots feature in GameViewController

* Updates project settings

* Passes call to save to Photos as a closure into authorization prompt
- Ensures that the screenshot is saved when the user is first prompted for access to Photos
- More elegant extension code

---------

Co-authored-by: Riley Testut <riley@rileytestut.com>
2023-04-28 14:35:26 -05:00

47 lines
1.2 KiB
Swift

//
// PHPhotoLibrary+Authorization.swift
// Delta
//
// Created by Chris Rittenhouse on 4/24/23.
// Copyright © 2023 Riley Testut. All rights reserved.
//
import UIKit
import Photos
extension PHPhotoLibrary
{
static func runIfAuthorized(code: @escaping () -> Void)
{
PHPhotoLibrary.requestAuthorization(for: .addOnly, handler: { success in
switch success
{
case .authorized:
code()
case .denied, .restricted, .notDetermined, .limited:
break
}
})
}
static func saveUIImage(image: UIImage)
{
// Save the image to the Photos app
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image)
}, completionHandler: { success, error in
if success
{
// Image saved successfully
print("Image saved to Photos app.")
}
else
{
// Error saving image
print("Error saving image: \(error?.localizedDescription ?? "Unknown error")")
}
})
}
}