guoyingtao / Mantis

An iOS Image cropping library, which mimics the Photo App written in Swift.
MIT License
921 stars 183 forks source link

Cropping returns original image #302

Closed danielsaidi closed 1 year ago

danielsaidi commented 1 year ago

Hi,

Thanks for a great library!

I'm building an OCR feature where the user should be able to select part of an image, and just noticed that some images fail to crop.

The cropping calls cropViewControllerDidCrop but with the original image instead of the cropped one:

image

For instance, regardless of which part of this podcast screenshot I select, I get the full image every time:

image

I use the following UIKit wrapper in SwiftUI:

struct ImageCropper: UIViewControllerRepresentable {

    var image: ImageRepresentable
    var completion: (CropResult) -> Void

    typealias CropResult = Result<ImageRepresentable, Error>

    @Environment(\.dismiss)
    var dismiss

    class Coordinator: CropViewControllerDelegate {

        init(_ cropper: ImageCropper) {
            self.cropper = cropper
        }

        var cropper: ImageCropper

        func cropViewControllerDidCrop(
            _ cropViewController: CropViewController,
            cropped: UIImage,
            transformation: Transformation,
            cropInfo: CropInfo
        ) {
            cropper.dismiss()
            cropper.completion(.success(cropped))
        }

        func cropViewControllerDidCancel(
            _ cropViewController: CropViewController,
            original: ImageRepresentable
        ) {
            cropper.dismiss()
        }

        func cropViewControllerDidFailToCrop(
            _ controller: CropViewController,
            original: ImageRepresentable) {}

        func cropViewControllerDidBeginResize(
            _ controller: CropViewController) {}

        func cropViewControllerDidEndResize(
            _ controller: CropViewController,
            original: ImageRepresentable,
            cropInfo: CropInfo) {}
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(
        context: Context
    ) -> CropViewController {
        let controller = Mantis.cropViewController(image: image)
        controller.delegate = context.coordinator
        return controller
    }

    func updateUIViewController(
        _ uiViewController: CropViewController,
        context: Context
    ) {}
}

Here is a screenshot from where I've placed a breakpoint in the CropView crop function:

image

Thankful for any help you can provide.

guoyingtao commented 1 year ago

@danielsaidi Thanks for the feedback. Can you post some images which failed to crop here then I can debug with them?

danielsaidi commented 1 year ago

Hi @guoyingtao

Sorry for the late reply.

To me, it happens with this image:

I was first unsure if uploading the image here would somehow alter it and make the problem disappear, but I've verified that the problem still happens after downloading this image.

guoyingtao commented 1 year ago

@danielsaidi Which version of Mantis are you using? I just tested 2.9.1 (the latest version) and it is working

danielsaidi commented 1 year ago

Hmmm this is strange.

I use SPM and added Mantis with the "Up to next major version" rule:

image

This however adds 1.9.0 and not 2.9.1:

image

I suspect that SPM doesn't work with the Mantis version naming convention, where a v is added before the version number. I also noticed that version tags aren't marked as GitHub releases, which may be required (although I'm not sure).

When I first tried adding v2.9.1 by branch, SPM first failed. This however turned out to be an "Xcode being Xcode" problem, where resetting the SPM cache made it work.

I can confirm that the cropping works great in 2.9.1 👍

If you want to discuss the version naming conventions, just lmk.

guoyingtao commented 1 year ago

@danielsaidi I noticed the setting for SPM is Up to next major version - 1.0.0 < 2.0.0, Maybe you can try change it to Up to next major version - 2.0.0 < 3.0.0

image
danielsaidi commented 1 year ago

@guoyingtao That was the default suggestion when I tried resolving the Mantis dependency. Entering 2.0.0 caused SPM to fail resolving the Mantis dependency altogether.

BUT...the strange thing is that when I cleared the SPM cache, things started working. This is very strange since I haven't used Mantis for a while and have cleared my cache many many times since then :)

danielsaidi commented 1 year ago

@guoyingtao

I found out why this behaved so strange. I accidentally used SSH instead of HTTPS when defining the package URL. The default resolve is still strange, but at least now I can type 2.0.0 to get the latest 2.9.1.

image