swift - 内存泄漏警报控制器 / UIImagePickerController
问题描述
我有一堂课要从画廊或相机中挑选一张照片。我打开一个 alertController 并让您在画廊和相机之间进行选择。它还包含一个委托。如果我从图库中选择一张图片,我会发现仪器存在内存泄漏。如何改进代码以避免内存泄漏?谢谢!
class ImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
weak var delegate: DelegatePhotohandler?
var viewController: UIViewController?
override init() {
super.init()
}
func pickImage<T:UIViewController>(viewController: T) {
self.viewController = viewController
let alertList = UIAlertController(title: NSLocalizedString("Load Picture", comment: "Picture alert Alertcontroller"), message: nil, preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) {
UIAlertAction in self.openCamera()
alertList.dismiss(animated: true, completion: nil)
}
let galleryAction = UIAlertAction(title: "Gallery", style: .default) {
UIAlertAction in self.openGallery()
alertList.dismiss(animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
UIAlertAction in
alertList.dismiss(animated: true, completion: nil)
}
alertList.addAction(cameraAction)
alertList.addAction(galleryAction)
alertList.addAction(cancelAction)
viewController.present(alertList, animated: true, completion: nil)
}
private func openCamera() {
if(UIImagePickerController .isSourceTypeAvailable(.camera)) {
imagePicker.sourceType = .camera
imagePicker.delegate = self
self.viewController!.present(imagePicker, animated: true, completion: nil)
} else {
let warningAlert = UIAlertController(title: "Warning", message: "You do not have a camera", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Okay", style: .cancel) {
UIAlertAction in
warningAlert.dismiss(animated: true, completion: nil)
}
warningAlert.addAction(cancelAction)
self.viewController!.present(warningAlert, animated: true, completion: nil)
}
}
private func openGallery() {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
self.viewController!.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[.originalImage] as? UIImage else {
print("Expected a dictionary containing an image, but was provided the following: \(info)")
return
}
delegate?.returnImage(image: image)
}
}
解决方案
UIImagePickerController
泄漏是一个已知且非常古老的问题:
解决方案是基本上将选取器的实例存储在单例中,并在需要时重用它(忽略选取器本身的小内存泄漏),或者使用不存在此问题的第三方图像选取器。
作为旁注,可以通过删除viewController
属性并将其作为参数添加到 and 中openCamera
来改进您的代码openGallery
,如下所示:
self.openGallery(viewController: viewController)
// ...
private func openGallery<T:UIViewController>(viewController: T) {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
viewController.present(imagePicker, animated: true)
}
推荐阅读
- html - flexbox 图像正在失去其在右侧的位置
- java - 用于不同 API 的相同 DTO 上的 Spring Boot 请求正文验证
- javascript - 为什么箭头功能在 chrome 扩展内容脚本中不起作用?
- r - 使用正常和反向 y 轴绘图
- android - 如何制作自定义的圆形剪裁器形状,例如内部边缘在颤动的凹口曲线?
- java - 得到错误org.springframework.http.converter.HttpMessageNotReadableException:无法构造java.time.LocalDate的实例
- java - 此行为可能是特定于 Windows 客户端操作系统的;请查看服务器日志或尝试 Linux 客户端
- javascript - 无法使用 Mongoose 从数据库中删除项目
- java - Spring JPA:从 EntityGraph 中的子项中排除 lob 字段
- c# - Automapper忽略嵌套属性