-1

苹果在 WWDC20 中引入了PHPicker,替代了 UIImagePickerController。

我有一个包装器 NSObject 类,它处理图像选择器控制器的所有配置和呈现,现在我正在替换实现以支持 iOS14。

即使我将委托设置为,self我也会收到错误:

[Picker] PHPickerViewControllerDelegate doesn't respond to picker:didFinishPicking:

我认为它会检查父视图控制器,确实它没有实现委托方法,但包装器实现了。

这是我的示例代码:

import Foundation
import PhotosUI

class myPicker: PHPickerViewControllerDelegate{
    
    func openFrom(parent:UIViewController!) {
        var config:PHPickerConfiguration! = PHPickerConfiguration()
        config.selectionLimit = 1
        config.filter = nil
        
        let pickerViewController:PHPickerViewController! = PHPickerViewController(configuration:config)
        pickerViewController.delegate = self //<---
        parent.present(pickerViewController, animated:true, completion:nil)
        
    }
    
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        
        picker.dismiss(animated: true, completion:nil)
        
        for result:PHPickerResult in results {
            let itemProvider:NSItemProvider = result.itemProvider
            print(itemProvider)
        }

// ...do something with images...
    }
}

使用它...

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let mypicker = myPicker()
        mypicker.openFrom(parent: self)
    }

你有什么建议?

4

2 回答 2

0

问题是 Cocoa Objective-C 不能自省你的类。改变

class myPicker: PHPickerViewControllerDelegate

class myPicker: NSObject, PHPickerViewControllerDelegate
于 2020-08-13T13:58:33.587 回答
0

您需要将“class myPicker”作为单例类。因为您应该确保委托不为零。此外,您可以在 didFinishPicking 方法中更改此实例 = nil。

于 2020-11-27T04:44:00.290 回答