【发布时间】:2022-01-26 04:40:57
【问题描述】:
我需要允许用户选择他们想要保存到稍后将检索的应用程序的图像。我已经在代码中有照片选择器,只是不知道如何保存和检索图像。
struct PhotoPicker: UIViewControllerRepresentable {
@Binding var Badge: UIImage
func makeUIViewController(context: Context) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
picker.allowsEditing = true
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
func makeCoordinator() -> Coordinator {
return Coordinator(photoPicker: self)
}
final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let photoPicker: PhotoPicker
init(photoPicker: PhotoPicker){
self.photoPicker = photoPicker
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage{
photoPicker.Badge = image
BadgeStatus.toggle()
}
picker.dismiss(animated: true)
}
}
}
【问题讨论】:
-
你的意思是,将图像写入文件,然后再读取?
-
是的,无论哪种方式解释得最好。
-
@workingdog 是的,无论哪种方式最好的解释