我正在使用静态UIImagePickerController
,它在应用程序打开时初始化,以避免浪费时间,cpu和内存以后创建更多内容。
因此,我想更改UIImagePickerController
的委托。每当我尝试设置委托时,print(imagePickerController.delegate)
打印nil,并且未设置委托。
我的委托类是这样的:
class ImagePickerDelegateForProfilePictureController: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
使用初始化程序创建它,该初始化程序提供对这样的配置文件图片控制器的引用:
MySingletonClass.imagePickerController.delegate = ImagePickerDelegateForProfilePictureController(profilePictureController: self)
我已实施didFinishPickingMediaWithInfo
和imagePickerControllerDidCancel
任何帮助将不胜感激!
编辑: 以下是我的代码的相关部分。 ImagePickerDelegate:
// MARK: - ImagePickerDelegateForProfilePictureController
class ImagePickerDelegateForProfilePictureController: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// The profile picture controller
var profilePictureController: ProfilePictureController?
// Initializer for the delegate
init(profilePictureController: ProfilePictureController) {
super.init()
self.profilePictureController = profilePictureController
}
// MARK: - Picker Controller Functions
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// Set profile picture
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// Dismiss the picker controller
}
}
ProfilePictureController中的相关代码:
lazy var profilePictureDelegate = ImagePickerDelegateForProfilePictureController(profilePictureController: self)
@objc func setDelegateAndPresentImagePicker() {
// Set the delegate
MySingletonClass.imagePickerController.delegate = profilePictureDelegate
// Prints nil
print(MySingletonClass.imagePickerController.delegate)
// Present the imagePicker
present(MySingletonClass.imagePickerController, animated: true, completion: nil)
}
imagePickerController在单身类中声明,如此
// imagePickerController: A standard image picker controller we can change the delegate of
static var imagePickerController: UIImagePickerController {
let ipc = UIImagePickerController()
ipc.mediaTypes = [kUTTypeImage as String] // import MobileCoreServices, Photos
return ipc
}