Swift XCode Image Picker / Crop Controller Goes Away After Selection and Zooms Out Screen

81 Views Asked by At

title.

Can't find anything like this bug online and I'm not sure what information I should include other than the methods used. Basically when I hit the plus I go to Add Image, then after selecting an image, cropping it, it brings me back to this screen and zooms out the view (adds those black bars) rather then returning me to the previous view

The other thing to note is this is a view that was brought up using present on the storyboard let obj = UIStoryboard.init(name: "SellMyStuff", bundle: nil).instantiateViewController(withIdentifier: "addMarketItemVC")as! AddMarketItemVC

This is the view after I select an image enter image description here

This is what it's supposed to bring me back to enter image description here

Code used to open Image Picker

func openImageSelectionAlert() {
        let alertMessage = UIAlertController(title: "Choose image", message: "", preferredStyle: .alert)
        let cameraAction = UIAlertAction(title: "Camera", style: .default) { (act) in
            if UIImagePickerController.isSourceTypeAvailable(.camera) {
                let imagePicker = UIImagePickerController()
                imagePicker.delegate = self
                imagePicker.sourceType = .camera;
                imagePicker.allowsEditing = true
                self.present(imagePicker, animated: true, completion: nil)
            }
            
        }
        alertMessage.addAction(cameraAction)
        let galleryAction = UIAlertAction(title: "Gallery", style: .default) { (act) in
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                let imagePicker = UIImagePickerController()
                imagePicker.delegate = self
                imagePicker.sourceType = .photoLibrary;
                imagePicker.allowsEditing = true
                self.present(imagePicker, animated: true, completion: nil)
            }
            
        }
        alertMessage.addAction(galleryAction)
        let action = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
        alertMessage .addAction(action)
        //alertMessage.view.tintColor = UIColor.red
        self.present(alertMessage, animated: true, completion: nil)
    }

CropViewController

extension AddMarketItemVC : CropViewControllerDelegate {
    public func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
        self.croppedRect = cropRect
        self.croppedAngle = angle
        updateImageViewWithImage(image, fromCropViewController: cropViewController)
    }
    
    public func cropViewController(_ cropViewController: CropViewController, didCropToCircularImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
        self.croppedRect = cropRect
        self.croppedAngle = angle
        updateImageViewWithImage(image, fromCropViewController: cropViewController)
    }
    
    public func updateImageViewWithImage(_ image: UIImage, fromCropViewController cropViewController: CropViewController) {
        
        
        if self.imageSelection==1 {
            self.imageItem1.image=image
            self.camera1image.isHidden = true
          
          //  self.imageItem1.contentMode = .scaleAspectFit
            
            if isFromEdit{
                self.updateElement(element: self.imageItem1.image, index: 0)
            }else{
                self.insertElementAtIndex(element: self.imageItem1.image , index: 0)
            }
            
            
        }else if self.imageSelection==2{
            self.imageItem2.image=image
            self.camera2image.isHidden = true
           // self.imageItem2.contentMode = .scaleAspectFit
            if isFromEdit{
                self.updateElement(element: self.imageItem2.image, index: 1)
            }else{
                self.insertElementAtIndex(element: self.imageItem2.image, index: 1)
            }
        }else{
            self.imageItem3.image=image
            self.camera3image.isHidden = true
           // self.imageItem3.contentMode = .scaleAspectFit
            if isFromEdit{
                self.updateElement(element: self.imageItem3.image, index: 2)
            } else {
                self.insertElementAtIndex(element: self.imageItem3.image , index: 2)
            }
        }
      
        print("Dismissed crop view controller")
        cropViewController.dismiss(animated: true, completion: nil)
        
        
  
    }
    
   
}

ImagePickerControllerDelegate

extension AddMarketItemVC : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    //MARK: - Image Picker -
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        //        let image = info[UIImagePickerControllerEditedImage]
        //        self.imageView_profileImage.image = image as? UIImage
        //        self.presenter?.uploadImage(image: image as! UIImage)
        //
        
        let image = info[UIImagePickerController.InfoKey(rawValue: UIImagePickerController.InfoKey.editedImage.rawValue)]
        
        
        let cropController = CropViewController.init(croppingStyle: .default, image: image as! UIImage)
        cropController.delegate = self
        
        picker.dismiss(animated: true, completion: {
            self.present(cropController, animated: true, completion: nil)
        })
    }
    
    func insertElementAtIndex(element: UIImage?, index: Int) {
       /* while self.imageArray.count <= index {
            self.imageArray.append(dummy_image)
        }
        */
        
        
        self.imageArray.insert(element!, at: index)
    }
    
    func updateElement(element: UIImage?,index: Int){
        self.imageArray[index] = element!
        
        if self.editImageArray.count > index + 1 {
            self.editImageArray[index].id = "0"
        }
        
        
    }
    
    
}
1

There are 1 best solutions below

0
Zach Handley On

Ugh it turns out the previous programmer used present to display it, i changed it to push a controller to the nav and it fixed the issue