1

I'm new to SwiftUI and I'm currently trying to implement a cropper image view to make sure that all images for my app are a standardized ratio. After I select the image from the image picker, I want to show the image cropper view where I have a determined image aspect ratio that I can change by moving around and zooming out. I've tried to find code online but almost everything relevant seem to be for the older versions of swift that I'm unsure how to reproduce with Swift UI.

Here is my image picker code for reference:



import SwiftUI
import PhotosUI

struct ImagePicker : UIViewControllerRepresentable {
    
    @Binding var picker : Bool
    @Binding var img_Data : Data
    
    func makeCoordinator() -> Coordinator {
        return ImagePicker.Coordinator(parent: self)
    }
    
    func makeUIViewController(context: Context) -> PHPickerViewController {
        
        var config = PHPickerConfiguration()
        config.selectionLimit = 1
        
        let controller = PHPickerViewController(configuration: config)
        
        controller.delegate = context.coordinator
        
        return controller
    }
    
    func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
        
        
    }
    
    class Coordinator : NSObject,PHPickerViewControllerDelegate{
        
        var parent : ImagePicker
        
        init(parent : ImagePicker) {
            self.parent = parent
        }
        
        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            
            if results.isEmpty{
                self.parent.picker.toggle()
                return
            }
            
            let item = results.first!.itemProvider
            
            if item.canLoadObject(ofClass: UIImage.self){
                
                item.loadObject(ofClass: UIImage.self) { (image, err) in
                    if err != nil{return}
                    
                    let imageData = image as! UIImage
                    
                    DispatchQueue.main.async {
                        self.parent.img_Data = imageData.jpegData(compressionQuality: 0.5)!
                        self.parent.picker.toggle()
                    }
                }
            }
        }
    }
}

I would like to crop the img_Data as necessary. If you could point me towards examples that have been done, that would be great! Thanks

Edit: I see this example on this GitHub repo: https://github.com/nkopilovskii/ImageCropper

I'm pretty confused as to how I can integrate this ImageCropper with my Swift Code based on the module initialization after I install the pods. Where would I go about initializing the view controller and displaying the navigation controller in my image picker code. Thanks for your help!

krish
  • 37
  • 2
  • 10
  • Thanks for the reply: https://stackoverflow.com/questions/59884297/swiftui-scale-zoom-and-crop-image This one helped but I downloaded the GitHub code in reference and it was really buggy with the view extending randomly from this GitHub: https://github.com/zhxf2012/CropImageView so it doesn't quite work – krish Jul 30 '21 at 09:33
  • What about this one.. https://stackoverflow.com/questions/63651077/how-to-center-crop-an-image-in-swiftui – Scriptable Jul 30 '21 at 09:51

0 Answers0