guoyingtao / Mantis

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

Delegate method returns uncroped image when cropping a screen shot of current PDView page #386

Closed SaiBalaji-PSS closed 5 months ago

SaiBalaji-PSS commented 6 months ago

I'm trying to take a screen shot of current pdf page displayed using PDFKit. And crop it further using Mantis,

Here is my screen shot code and I'm passing the image to mantis. But the mantis delegate method
func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) returns uncropped image as output.


  @objc func takeScreenShot(){
        if let pdfPage = pdfView.currentPage, let pdfDocument = pdfView.document {
            let pdfPageRect = pdfPage.bounds(for: .mediaBox)
            let renderer = UIGraphicsImageRenderer(size: pdfPageRect.size)
            let pimage = renderer.image { ctx in
                UIColor.white.set()
                ctx.fill(pdfPageRect)
                ctx.cgContext.translateBy(x: 0.0, y: pdfPageRect.size.height)
                ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
                pdfPage.draw(with: .cropBox, to: ctx.cgContext)
            }

           //  Present cropping view
            let cropViewController = Mantis.cropViewController(image: pimage)
            cropViewController.delegate = self
            cropViewController.modalPresentationStyle = .fullScreen // Ensure it's fullscreen
            present(cropViewController, animated: true)

        }
    }

Here I have cropped particular part of the screen shot image of pdf page Simulator Screenshot - iPhone 15 Pro - 2024-03-25 at 22 49 13 But when loading the cropped image on UIImage it gives uncropped image

extension ReaderVC: CropViewControllerDelegate{
    func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
        cropped.saveToPhotoLibrary(nil, nil)
        print(cropInfo)
        self.dismiss(animated: true)
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TestVC") as! TestVC
        vc.croppedImage = cropped
        self.present(vc, animated: true)

    }

    func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
        self.dismiss(animated: true)
    }
    func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage) {
        print("ERROR")
    }

}

Simulator Screenshot - iPhone 15 Pro - 2024-03-25 at 22 49 19

guoyingtao commented 6 months ago

@SaiBalaji-PSS Would you mind posting the original screenshot you made from your pdf here so I can have a test?

SaiBalaji-PSS commented 6 months ago

Update: I have tested it with another PDF file which has only one page of A4 size it works well. The issue occurs trying to crop a PDF page which is rectangular in size with two A4 pages joined together. The screen shot size was 36.4 × 25.71 cm which fails to crop.

guoyingtao commented 6 months ago

Can you put some break points at line 27/64/111 in CGImageExtensions.swift to see if it threw some error or went into default branch in getBackupBitmapInfo.

SaiBalaji-PSS commented 6 months ago

It went into (64,16) branch

Screenshot 2024-03-27 at 8 52 20 PM
guoyingtao commented 6 months ago

@SaiBalaji-PSS Can you also check if it throws any errors in the same file. You can search ‘throw’ in the same file to locate the code. What’s your version of Mantis? Can you upgrade it to the latest version?

guoyingtao commented 5 months ago

@SaiBalaji-PSS Since I haven't heard from you for a while, I close this ticket for now. You can reopen it any time if needed.