SWIFT 4.2:IGRPhotoTweakscropAction和CIPerspectiveCorrection

时间:2019-03-05 22:54:01

标签: ios swift uiimage

IOS 11 +,Swift 4.2。我正在使用IGRPhotoTweaks进行裁剪和旋转。它完全可以满足这些需求。

现在,我正在尝试合并透视校正和一些迷失。看起来很简单,我可以让两个人分开工作,但不能一起工作。

我的过程是:

在我的票证编辑视图控制器中:

 extension TicketEditViewController: IGRPhotoTweakViewControllerDelegate {
    func photoTweaksController(_ controller: IGRPhotoTweakViewController, didFinishWithCroppedImage croppedImage: UIImage) {
        _ = controller.navigationController?.popViewController(animated: true)

        self.takenImage = croppedImage

    }

    func photoTweaksControllerDidCancel(_ controller: IGRPhotoTweakViewController) {
        _ = controller.navigationController?.popViewController(animated: true)
    }
}

当我得到didFinishWithCroppedImage时,我得到了croppedImage,然后是原始图像。

问题是:然后我如何对已裁剪到票证中某个区域的票证的裁剪图像应用透视图?

这是我的代码段(摘自https://github.com/kronik/smartcrop)。它可以在不同背景下处理完整票证,但是当我尝试仅传递票证的裁剪区域时,它不会获得任何features()。

    let docImage = CIImage(image: originalImage!)!
    var rect: CIRectangleFeature = CIRectangleFeature()        
    if let detector = CIDetector(ofType: CIDetectorTypeRectangle,
                                 context: ciContext,
                                 options: [CIDetectorAccuracy: CIDetectorAccuracyHigh]) {

        let features = detector.features(in: docImage)
        if features.first != nil {
        rect = detector.features(in: docImage).first as! CIRectangleFeature
        } else {
            nextStepButton.setTitle("Error, no points detected", for: .normal)
            return
        }
    }

    let perspectiveCorrection = CIFilter(name: "CIPerspectiveCorrection")!
    let docImage = CIImage(image: originalImage!)!

    perspectiveCorrection.setValue(CIVector(cgPoint:rect.topLeft),
                                   forKey: "inputTopLeft")
    perspectiveCorrection.setValue(CIVector(cgPoint:rect.topRight),
                                   forKey: "inputTopRight")
    perspectiveCorrection.setValue(CIVector(cgPoint:rect.bottomRight),
                                   forKey: "inputBottomRight")
    perspectiveCorrection.setValue(CIVector(cgPoint:rect.bottomLeft),
                                   forKey: "inputBottomLeft")
    perspectiveCorrection.setValue(docImage,
                                   forKey: kCIInputImageKey)

    let outputImage = perspectiveCorrection.outputImage

    let updatedImage = UIImage(ciImage: outputImage!, scale: originalImage!.scale, orientation: .up)

如何从IGRTweaks的UIImage(croppedImage)获取正确的“矩形”并将其传递给PerspectiveCorrection?

0 个答案:

没有答案