-1

I am using this Photo Helper to pick pictures from phone or take a photo but every time I use it, it adds up to the memory use. The image is uploaded to Firebase and it doesn't have to be held in the memory anyway. How can I stop my app to stop adding up memory use every time an image is picked?

import 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)
    }
}

tabbar middle button:

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController.title == "middleButton" {
        photoHelper.presentActionSheet(from: self)
        
        return false
    } else {
        return true
    }
}
iPancake
  • 109
  • 1
  • 9
  • 1
    `picker.pushViewController`? Really? Your job here is to dismiss the picker, not to keep it and push something else onto it. No wonder the memory grows. – matt Jul 02 '22 at 09:48
  • Okay that makes sense. but how can I push a view controller after image is selected? This photo helper is triggered to show UIImagePickerController from a tab of a tabbar controller. – iPancake Jul 02 '22 at 13:43
  • 1
    I don't know what you _want_ to do or what your overall architecture is — because you didn't tell me. What you asked is why the memory grows each time you present an image picker, and I'm telling you why: it's because that picker subsequently lives forever. You're just piling up view controllers on top of each other. Replace `picker.pushViewController` with `picker.dismiss` and the problem is solved. What _else_ you do, is _your_ affair. – matt Jul 02 '22 at 14:04
  • I wannat push the view controller from tabbar but after the image is selected because I wanna pass the image to that view controller. I added the code where I show the image picker. – iPancake Jul 02 '22 at 14:23
  • 1
    Fine but _that is not what you asked_. The question is about memory usage and now you know the answer. – matt Jul 02 '22 at 14:24
  • https://stackoverflow.com/questions/72839967/uiimagepickercontroller-how-to-push-from-middle-button-of-tabbar-after-image-is – iPancake Jul 02 '22 at 14:46

1 Answers1

0

I used a protocol to pass the image from the photo helper to the tabBar class, where I present the view controller. The method looks something like this:

  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)
    }

And, since this is dismissed and picker controller doesn't have to push or present anything, it doesn't cause memory to grow anymore.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
iPancake
  • 109
  • 1
  • 9