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!