使用CGAffineTransform组合两个图像

时间:2017-07-03 16:41:59

标签: swift ios11 swift4 apple-vision

我正在使用新的Apple Vision API的VNImageTranslationAlignmentObservation来返回CGAffineTransform。这个想法是你传递两个可以合并在一起的图像并返回CGAffineTransform,这样你就可以这样做。我已经设法让代码工作,以便我得到一个CGAffineTransform返回,但经过大量阅读我不知道如何我可以合并两个图像与信息。

我的代码在这里:

import UIKit
import Vision

class ImageTranslation {

    let referenceImage: CGImage!
    let floatingImage: CGImage!
    let imageTranslationRequest: VNTranslationalImageRegistrationRequest!

    init(referenceImage: CGImage, floatingImage: CGImage) {
        self.referenceImage = referenceImage
        self.floatingImage = floatingImage
        self.imageTranslationRequest = VNTranslationalImageRegistrationRequest(targetedCGImage: floatingImage, completionHandler: nil)
    }


    func handleImageTranslationRequest() -> UIImage {
        var alignmentTransform: CGAffineTransform!
        let vnImage = VNSequenceRequestHandler()
        try? vnImage.perform([imageTranslationRequest], on: referenceImage)
        if let results = imageTranslationRequest.results as? [VNImageTranslationAlignmentObservation] {
            print("Image Transformations found \(results.count)")
            results.forEach { result in
                alignmentTransform = result.alignmentTransform

                print(alignmentTransform)
            }
        }

        return applyTransformation(alignmentTransform)

    }

    private func applyTransformation(_ transform: CGAffineTransform) -> UIImage {
        let image = UIImage(cgImage: referenceImage)
        return image
    }
}

我得到的打印转换就像CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 672.0, ty: 894.0)

如何将这两张传入的图片应用于这两张?

0 个答案:

没有答案