guoyingtao / Mantis

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

Continue editing. #362

Closed yangKJ closed 10 months ago

yangKJ commented 10 months ago

你好, 这边支持,退出后下次再次进入继续上次编辑信息吗? 比如进行过旋转和翻转等操作。

yangKJ commented 10 months ago

看到你库里有个属性presetTransformationType 再请问一下,我怎么样实时拿去到这个Transformation呢?

guoyingtao commented 10 months ago

@yangKJ CropViewControllerDelegate中的cropViewControllerDidCrop会输出Transformation信息 另外你可以参考Example工程中的presentWithPresetTransformation函数

yangKJ commented 10 months ago

你好,这个是执行crop()裁剪函数之后才会响应的方法, 我其实想要的是只要发生改变就能得到新的Transformation信息这种需求

yangKJ commented 10 months ago

这个是我这边的代码;

struct ImageCropper: UIViewControllerRepresentable {
    @ObservedObject var image: Published_Image
    @Binding var crop: Crop
    @Binding var croped: Bool
    @Binding var transformation: Mantis.Transformation
    var onCroped: Crop.CropedComplete
    private var lastCrop: Crop?

    init(image: UIImage, crop: Binding<Crop>, croped: Binding<Bool>, transformation: Binding<Transformation>, onCroped: @escaping Crop.CropedComplete) {
        self.image = Published_Image(image)
        self._crop = crop
        self._croped = croped
        self._transformation = transformation
        self.onCroped = onCroped
        self.lastCrop = crop.wrappedValue
    }

    func makeUIViewController(context: Context) -> CropViewController {
        var config = Mantis.Config()
        config.showAttachedCropToolbar = false
        config.cropViewConfig.cropShapeType = crop.shapeType
        switch crop.ratio {
        case .free:
            config.presetFixedRatioType = .canUseMultiplePresetFixedRatio(defaultRatio: 0)
        case .origin:
            let ratio = image.image.size.width / image.image.size.height
            config.presetFixedRatioType = .alwaysUsingOnePresetFixedRatio(ratio: ratio)
        default:
            config.presetFixedRatioType = .alwaysUsingOnePresetFixedRatio(ratio: crop.ratio.whRatio)
        }
        config.cropViewConfig.cropMaskVisualEffectType = .custom(color: R.color.app_black.uicolor!.withAlphaComponent(0.3))
        config.cropViewConfig.backgroundColor = .clear
        if compare(trans0: transformation, trans1: Crop.zero) {
            config.cropViewConfig.presetTransformationType = .none
        } else {
            config.cropViewConfig.presetTransformationType = .presetInfo(info: transformation)
        }
        //config.cropViewConfig.builtInRotationControlViewType = .slideDial(config: .init())
        //config.cropToolbarConfig.mode = .embedded
        //config.cropToolbarConfig.toolbarButtonOptions = .reset
        let cropViewController = Mantis.cropViewController(image: image.image, config: config)
        cropViewController.delegate = context.coordinator
        cropViewController.view.backgroundColor = .clear
        return cropViewController
    }

    func updateUIViewController(_ uiViewController: CropViewController, context: Context) {
        if croped {
            uiViewController.crop()
            croped = false
        }
        guard let lastCrop = lastCrop else {
            return
        }
        if lastCrop.horizontallyFlipped != crop.horizontallyFlipped {
            uiViewController.didSelectHorizontallyFlip()
        }
        if lastCrop.verticallyFlipped != crop.verticallyFlipped {
            uiViewController.didSelectVerticallyFlip()
        }
    }

    class Coordinator {
        var parent: ImageCropper
        init(_ parent: ImageCropper) {
            self.parent = parent
        }
    }

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

extension ImageCropper.Coordinator: CropViewControllerDelegate {
    func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController) {
        // 此方法能获取到实时的Transformation吗?
    }

    func cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation, cropInfo: CropInfo) {
        parent.transformation = transformation
        parent.onCroped(parent.crop, cropped, transformation)
    }

    func cropViewControllerDidCancel(_ cropViewController: CropViewController, original: UIImage) {

    }

    func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage) {

    }

    func cropViewControllerDidBeginResize(_ cropViewController: CropViewController) {

    }

    func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo) {

    }
}
yangKJ commented 10 months ago

我会在使用ImageCropper时强制刷新UI,然后会再次走makeUIViewController 但是这时我需要我刷新UI前做的操作,所以有个这样的需求。 谢谢大佬,或者你能开放一下我获取到这个输出Transformation信息吗?

yangKJ commented 10 months ago

大佬能麻烦你一下,在CropViewController放一个读取Transformation信息的可读属性吗? 研读了您的代码,是不是把您的makeTransformation开放出来就可以了!?

guoyingtao commented 10 months ago

@yangKJ 你试一下 PR #363 看看是否解决了你的问题

yangKJ commented 10 months ago

好的,谢谢

yangKJ commented 10 months ago

大佬,其实你这边可以只需要在 CropViewController 开放 cropView.makeTransformation() 属性出来就完全可以了,这样也不用新增加协议方法的。

guoyingtao commented 10 months ago

CropView目前对于用户是不可见的。用户和Mantis的交互主要是通过CropViewController以及CropViewControllerDelegate实现的

CropViewControllerDelegate中已经有了一个cropViewControllerDidImageTransformed方法,之前并没有输出任何Transformation信息,所以增加cropViewControllerDidImageTransformed(_ cropViewController: CropViewController, transformation: Transformation)我认为是更加合适的。在增加这个方法后,我已经标记了原来的·cropViewControllerDidImageTransformed·为deprecated。

yangKJ commented 10 months ago

这边还有个问题是需要在func updateUIViewController(_ uiViewController: CropViewController, context: Context)当中获取到当前的Transformation信息

guoyingtao commented 10 months ago

updateUIViewController应该只在特定的几个时机调用吧。 如果暂存一下从cropViewControllerDidImageTransformed(_ cropViewController: CropViewController, transformation: Transformation)获取的信息,然后在updateUIViewController中使用暂存的信息,是否可以解决问题?

yangKJ commented 10 months ago

ok. 感谢!

yangKJ commented 10 months ago

你好,这边可以合并到主分支,然后提交到SPM吗?

guoyingtao commented 10 months ago

@yangKJ 我刚刚发布了Mantis 2.18.0,里面包含了你需要的特性。