Memory leaks 这是在SwiftUI中使用PHPicker的正确方法吗?因为我';我有很多漏洞
我正在试图弄清楚是我的代码导致了这个问题,还是我应该向苹果提交一份bug报告 在一个新项目中,我有以下代码: ContentView() SystemImagePicker.swift 但是,当只选择一个图像时(根据我的代码,不选择,然后“改变主意”,然后选择另一个不同的图像),我在Xcode中运行内存图时会发现这些漏洞 这是我的代码,还是苹果的 无论如何,imagepicker上的Memory leaks 这是在SwiftUI中使用PHPicker的正确方法吗?因为我';我有很多漏洞,memory-leaks,swiftui,uiimagepickercontroller,phpicker,Memory Leaks,Swiftui,Uiimagepickercontroller,Phpicker,我正在试图弄清楚是我的代码导致了这个问题,还是我应该向苹果提交一份bug报告 在一个新项目中,我有以下代码: ContentView() SystemImagePicker.swift 但是,当只选择一个图像时(根据我的代码,不选择,然后“改变主意”,然后选择另一个不同的图像),我在Xcode中运行内存图时会发现这些漏洞 这是我的代码,还是苹果的 无论如何,imagepicker上的取消按钮也不起作用。因此,用户不能仅仅关闭选择器工作表,必须选择一个图像来关闭工作表 关于旧UIImagePic
取消
按钮也不起作用。因此,用户不能仅仅关闭选择器工作表,必须选择一个图像来关闭工作表
关于旧UIImagePickerController的进一步说明
以前,我将此代码用于旧的UIImagePickerController
import SwiftUI
struct ImagePicker: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentationMode
@Binding var image: UIImage?
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let parent: ImagePicker
init(_ parent: ImagePicker) {
self.parent = parent
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let uiImage = info[.originalImage] as? UIImage {
parent.image = uiImage
}
parent.presentationMode.wrappedValue.dismiss()
}
deinit {
print("deinit")
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
}
}
导入快捷界面
结构图像选择器:UIViewControllerRepresentable{
@环境(\.presentationMode)变量presentationMode
@绑定var映像:UIImage?
类协调器:NSObject、UINavigationControllerDelegate、UIImagePickerControllerDelegate{
让父对象:ImagePicker
init(uu父对象:ImagePicker){
self.parent=parent
}
func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo:[UIImagePickerController.InfoKey:Any]){
如果让uiImage=info[.originalImage]作为?uiImage{
parent.image=uiImage
}
parent.presentationMode.wrappedValue.disclease()
}
脱硝{
印刷品(“脱硝”)
}
}
func makeCoordinator()->Coordinator{
协调员(自我)
}
func makeUIViewController(上下文:UIViewControllerRepresentableContext)->UIImagePickerController{
让picker=UIImagePickerController()
picker.delegate=context.coordinator
回程选择器
}
func updateUIViewController(uViewController:UIImagePickerController,上下文:UIViewControllerRepresentableContext){
}
}
这也会导致在选择图像时出现泄漏,但其数量要少得多:
import SwiftUI
struct SystemImagePicker: UIViewControllerRepresentable {
@Environment(\.presentationMode) private var presentationMode
@Binding var image: UIImage?
func makeUIViewController(context: Context) -> PHPickerViewController {
var configuration = PHPickerConfiguration()
configuration.selectionLimit = 1
configuration.filter = .images
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
class Coordinator: NSObject, PHPickerViewControllerDelegate {
let parent: SystemImagePicker
init(parent: SystemImagePicker) {
self.parent = parent
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
for img in results {
guard img.itemProvider.canLoadObject(ofClass: UIImage.self) else { return }
img.itemProvider.loadObject(ofClass: UIImage.self) { image, error in
if let error = error {
print(error)
return
}
guard let image = image as? UIImage else { return }
self.parent.image = image
self.parent.presentationMode.wrappedValue.dismiss()
}
}
}
}
}
import SwiftUI
struct ImagePicker: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentationMode
@Binding var image: UIImage?
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let parent: ImagePicker
init(_ parent: ImagePicker) {
self.parent = parent
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let uiImage = info[.originalImage] as? UIImage {
parent.image = uiImage
}
parent.presentationMode.wrappedValue.dismiss()
}
deinit {
print("deinit")
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
}
}