julian
julian

Reputation: 157

UIImagePickerControllerDelegate not being called, selected image never displayed

I'm stumped. Everything is working except it's not. I can call up the camera interface and take or select a photo yet UIImagePickerControllerDelegate is never being called (I've put two print statements that well, never get printed). Everything is correctly connected in IB, or so I believe it is (I'm fully ready for it so be something totally minor that I missed). Anyone care to take a frtesh set of eyes and go over this to see if I just can't see the forest for the trees?

Thanks in advance.

import UIKit
import Foundation


class afterPhotoViewController: UIViewController {

    //MARK: - IBOutlet Properties
    @IBOutlet weak var afterImageView: UIImageView!
    @IBOutlet weak var aftervwImage: UIView!
    
    @IBOutlet weak var cameraBtnPressed: UIButton!
    
    //MARK: - Useful Variables
    let afterImagePicker = UIImagePickerController()
    var hud = HKProgressHUD()
    var localTextFilePath : URL!
    var isImageSetted = false

    //MARK: - App Delegates
    var isMain = false
    var originFrame = CGRect()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Init UI Components
        self.initUI()
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: - UI Init
    func initUI() {
        // Set Navigation Bar
        let backButton = UIBarButtonItem(title: "", style: .plain, target: navigationController, action: nil)
        navigationItem.leftBarButtonItem = backButton
        self.title = "\(h_proposalNumberStr)-\(h_itemIndex)"
        
        // Set ImageView
        self.afterImageView.image = #imageLiteral(resourceName: "ic_placeholder")
        self.afterImageView.clipsToBounds = false
        
        
        cameraBtnPressed.layer.cornerRadius = 15
        cameraBtnPressed.layer.borderColor = UIColor.lightGray.cgColor
        cameraBtnPressed.layer.borderWidth = 1
        
        self.isImageSetted = false
        
       // self.tableView.reloadData()
        
    }
    
    //MARK: - UI Actions
    @IBAction func cameraBtnPressed(_ sender: Any) {
        //Create the AlertController and add Its action like button in Actionsheet
        let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        
        let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { _ in
        }
        actionSheetController.addAction(cancelActionButton)
        
        let chooseLibraryActionButton = UIAlertAction(title: "Choose from Library", style: .default)
        { _ in
            self.isMain = true
            self.afterImagePicker.allowsEditing = true
            self.afterImagePicker.sourceType = .photoLibrary
            self.afterImagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
            self.present(self.afterImagePicker, animated: true, completion: nil)
        }
        actionSheetController.addAction(chooseLibraryActionButton)
        
        let takePhotoActionButton = UIAlertAction(title: "Take a Photo", style: .default)
        { _ in
            if UIImagePickerController.isSourceTypeAvailable(.camera) {
                self.isMain = true
                self.afterImagePicker.allowsEditing = false
                self.afterImagePicker.sourceType = .camera
                self.afterImagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera)!
                self.afterImagePicker.showsCameraControls = true
                self.present(self.afterImagePicker, animated: true, completion: nil)
            } else {
                Helper.showMessage(vc: self, title: CONSTANTS.APPINFO.APP_NAME, bodyStr: "No camera available.")
            }
        }
        actionSheetController.addAction(takePhotoActionButton)
        
        self.present(actionSheetController, animated: true, completion: nil)
    }
   
    
//MARK: - UIImagePickerControllerDelegate
extension afterPhotoViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print("called")
        dismiss(animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print("called2")
        //getting actual image
        var image = info[UIImagePickerControllerEditedImage] as? UIImage

        if image == nil {
            image = info[UIImagePickerControllerOriginalImage] as? UIImage
        }

            afterImageView.image = image
            // Set Imageview Corner Radius
            afterImageView.layer.cornerRadius = 5
            afterImageView.clipsToBounds = true

            self.isImageSetted = true

        picker.dismiss(animated: true, completion: nil)
    }
}

Upvotes: 0

Views: 36

Answers (2)

JoeGalind
JoeGalind

Reputation: 3805

You have to specify that the UIImagePickerController's delegate is self, so that the events are triggered.

In your case:

afterImagePicker.delegate = self

Upvotes: 1

julian
julian

Reputation: 157

@raja Kishan caught my stupid simple error of forgetting to set the delegate.

Now, where's the facepalm emoji.

Upvotes: 1

Related Questions