PHPhotoLibrary.shared().performChanges crashes when trying to save image to photo library
I'm using PHPhotoLibrary.shared().performChanges {} to save an image to the photo library. The code works at Swift 5, however when switching to Swift 6 and clicking the save button I'm receiving the following error message:
IOSurfaceClientSetSurfaceNotify failed e00002c7
import SwiftUI
import Photos
struct ContentView: View {
var body: some View {
VStack {
Color.blue
.frame(width: 100, height: 100)
Button("Save") {
if let imageData = ImageRenderer(content: self.body).uiImage?.pngData() {
saveImageToPhotoLibrary(imageData: imageData)
}
}
}
}
private func saveImageToPhotoLibrary(imageData: Data) {
Task {
let status = await PHPhotoLibrary.requestAuthorization(for: .addOnly)
guard status == .authorized else { return }
try await PHPhotoLibrary.shared().performChanges {
let request = PHAssetCreationRequest.forAsset()
request.addResource(with: .photo, data: imageData, options: nil)
}
}
}
}