In my SwiftUI software consumer ought to is allowed choose just one photograph from photograph gallery. When first time software is put in and it asks for App wish to entry your photograph libary pop up and consumer selects restricted entry at in photograph record consumer can choose a number of images(although it could not be chosen on click on of accomplished). I need to prohibit it to deciding on solely photograph is it potential. Under code works high quality in all different eventualities. I feel as it’s iOS generated permission popup such restriction wouldn’t be potential. Any assistance on this could be appreciated.
import SwiftUI
import PhotosUI
struct ContentView: View {
@State var imagePickerIsPresented: Bool = false
@State var imageWasImported: Bool = false
@State var picture: UIImage = UIImage()
var physique: some View {
VStack {
Picture(uiImage: picture)
.resizable()
.body(width: 100,peak: 150)
Button("Choose Picture") {
self.requestAccess()
}
}
.padding()
.sheet(isPresented: $imagePickerIsPresented, content material: {
ImagePicker(imageToImport: $picture, isPresented: $imagePickerIsPresented, imageWasImported: $imageWasImported)
})
}
func requestAccess() {
let accessLevel: PHAccessLevel = .readWrite
let standing = PHPhotoLibrary.authorizationStatus(for: accessLevel)
change standing {
case .approved:
self.imagePickerIsPresented.toggle()
case .restricted:
print("Restricted entry - present picker.")
self.imagePickerIsPresented.toggle()
case .denied:
print("Entry denied")
case .notDetermined:
PHPhotoLibrary.requestAuthorization { newStatus in
change newStatus {
case .approved:
print("Full entry.")
case .restricted:
print("Restricted entry.")
break
case .denied:
break
default:
break
}
}
default:
break
}
}
}
#Preview {
ContentView()
}
import SwiftUI
import PhotosUI
struct ImagePicker: UIViewControllerRepresentable {
@Binding var imageToImport: UIImage
@Binding var isPresented: Bool
@Binding var imageWasImported: Bool
func makeUIViewController(context: UIViewControllerRepresentableContext) -> some UIViewController {
var configuration = PHPickerConfiguration()
configuration.filter = .pictures
configuration.selectionLimit = 1
let imagePicker = PHPickerViewController(configuration: configuration)
imagePicker.delegate = context.coordinator
return imagePicker
}
func updateUIViewController(_ uiViewController: ImagePicker.UIViewControllerType, context: UIViewControllerRepresentableContext) {}
func makeCoordinator() -> ImagePicker.Coordinator {
return Coordinator(mother or father: self)
}
class Coordinator: NSObject, PHPickerViewControllerDelegate {
var mother or father: ImagePicker
init(mother or father: ImagePicker) {
self.mother or father = mother or father
}
func picker(_ picker: PHPickerViewController, didFinishPicking outcomes: [PHPickerResult]) {
picker.dismiss(animated: true)
if outcomes.depend != 1 {
return
}
if let picture = outcomes.first {
if picture.itemProvider.canLoadObject(ofClass: UIImage.self) {
picture.itemProvider.loadObject(ofClass: UIImage.self) { picture, error in
if let picture = picture {
self.mother or father.imageToImport = picture as! UIImage
self.mother or father.imageWasImported.toggle()
}
}
}
}
self.mother or father.isPresented.toggle()
}
}
}