我已经使用PHPicker来查看get五月图像。我想问一下,当你选择一个图像时,你如何只显示选定的图像?我已经尝试了很多东西,它总是显示所有的图像在加利利。帮帮忙吧。


以下是“视图代码”:
import Foundation
import PhotosUI
import SwiftUI
struct ImagePicker: UIViewControllerRepresentable {
@Binding var imageToImport: UIImage?
@Binding var isPresented: Bool
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> some UIViewController {
var configuration = PHPickerConfiguration()
configuration.filter = .images
configuration.selectionLimit = 1
let imagePicker = PHPickerViewController(configuration: configuration)
imagePicker.delegate = context.coordinator
return imagePicker
}
func updateUIViewController(_ uiViewController: ImagePicker.UIViewControllerType, context: UIViewControllerRepresentableContext<ImagePicker>) {}
func makeCoordinator() -> ImagePicker.Coordinator {
return Coordinator(parent: self)
}
class Coordinator: NSObject, PHPickerViewControllerDelegate {
var parent: ImagePicker
init(parent: ImagePicker) {
self.parent = parent
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
if results.count != 1 {
return
}
if let image = results.first {
if image.itemProvider.canLoadObject(ofClass: UIImage.self) {
image.itemProvider.loadObject(ofClass: UIImage.self) { image, error in
if let image = image {
self.parent.imageToImport = image as? UIImage
}
}
}
}
self.parent.isPresented.toggle()
}
}
}这里的图像拾荒者:
import Foundation
import PhotosUI
import SwiftUI
struct ImagePicker: UIViewControllerRepresentable {
@Binding var imageToImport: UIImage?
@Binding var isPresented: Bool
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> some UIViewController {
var configuration = PHPickerConfiguration()
configuration.filter = .images
configuration.selectionLimit = 1
let imagePicker = PHPickerViewController(configuration: configuration)
imagePicker.delegate = context.coordinator
return imagePicker
}
func updateUIViewController(_ uiViewController: ImagePicker.UIViewControllerType, context: UIViewControllerRepresentableContext<ImagePicker>) {}
func makeCoordinator() -> ImagePicker.Coordinator {
return Coordinator(parent: self)
}
class Coordinator: NSObject, PHPickerViewControllerDelegate {
var parent: ImagePicker
init(parent: ImagePicker) {
self.parent = parent
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
if results.count != 1 {
return
}
if let image = results.first {
if image.itemProvider.canLoadObject(ofClass: UIImage.self) {
image.itemProvider.loadObject(ofClass: UIImage.self) { image, error in
if let image = image {
self.parent.imageToImport = image as? UIImage
}
}
}
}
self.parent.isPresented.toggle()
}
}
}我只想显示用户只选择的图片。如果你能帮我的话,谢谢。

发布于 2022-11-18 13:27:41
let viewController = // The UIViewController from which to present the picker.
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: viewController)或
let viewController = // The UIViewController from which to present the picker.
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: viewController) { identifiers in
for newlySelectedAssetIdentifier in identifiers {
// Stage asset for app interaction.
}
}https://stackoverflow.com/questions/74489756
复制相似问题