Swift XCode图像拾取器/裁剪控制器在选择和缩小屏幕后消失

zynd9foi  于 2023-03-17  发布在  Swift
关注(0)|答案(1)|浏览(102)

标题。
在网上找不到类似的错误,我也不确定除了使用的方法之外,我还应该包括哪些信息。基本上,当我点击加号时,我会进入添加图像,然后在选择一个图像后,裁剪它,它会把我带回这个屏幕并缩小视图(添加那些黑条),而不是返回到以前的视图
另一件需要注意的事情是,这是使用序列图像板let obj = UIStoryboard.init(name: "SellMyStuff", bundle: nil).instantiateViewController(withIdentifier: "addMarketItemVC")as! AddMarketItemVC上的present创建的视图
这是我选择图像

后的视图
这就是它应该把我带回到

的地方
用于打开图像选取器的代码

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

裁剪视图控制器

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

图像拾取器控制器委托

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"
        }
        
        
    }
    
    
}
ttp71kqs

ttp71kqs1#

原来之前的程序员用present来显示它,我把它改成把一个控制器推到导航上,它修复了这个问题

相关问题