我使用这个照片助手从手机中挑选照片或拍照,但每次我使用它时,它都会增加内存使用。图像被上传到Firebase,它不需要保存在内存中。我如何停止我的应用程序,以停止每次选择图像时增加内存使用?
进口UIKit
class MGPhotoHelper: NSObject {
// MARK: - Properties
var completionHandler: ((UIImage) -> Void)?
let imagePickerController = UIImagePickerController()
// MARK: - Helper Methods
func presentActionSheet(from viewController: UIViewController) {
let alertController = UIAlertController(title: nil, message: "Take Pic or Pick From Phone", preferredStyle: .actionSheet)
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let capturePhotoAction = UIAlertAction(title: "Take Photo", style: .default, handler: { action in
self.presentImagePickerController(with: .camera, from: viewController)
})
alertController.addAction(capturePhotoAction)}
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let uploadAction = UIAlertAction(title: "Upload from Phone", style: .default, handler: { action in
self.presentImagePickerController(with: .photoLibrary, from: viewController)
})
alertController.addAction(uploadAction)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
viewController.present(alertController, animated: true)
}
func presentImagePickerController(with sourceType: UIImagePickerController.SourceType, from viewController: UIViewController) {
imagePickerController.sourceType = sourceType
viewController.present(imagePickerController, animated: true)
imagePickerController.delegate = self
}
}
extension MGPhotoHelper: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImage = info[.originalImage] as? UIImage {
completionHandler?(selectedImage)
let postDeal = PostDealVC()
postDeal.dealImage.image = selectedImage
picker.setNavigationBarHidden(false, animated: false)
picker.pushViewController(postDeal, animated: true)
} else {
return
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
}
标签栏中间按钮:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController.title == "middleButton" {
photoHelper.presentActionSheet(from: self)
return false
} else {
return true
}
}
我使用一个协议将图像从照片助手传递到tabBar
类,在那里我呈现视图控制器。该方法看起来像这样:
func pushPostDealVC(image: UIImage) {
print("push post deal VC ")
postDealVC.dealImage.image = image
postDealVC.modalPresentationStyle = .fullScreen
present(postDealVC, animated: true, completion: nil)
self.dismiss(animated: true, completion: nil)
}
并且,由于这个被解除,picker控制器不需要推送或呈现任何东西,它不会再导致内存增长。