MacOS swift3将两个图像拼接在一起

时间:2017-01-15 23:13:45

标签: swift3 macos-sierra nsimage cifilter

我喜欢在xcode中使用swift 3拼接两个NSImages。 我能够使用“CIAdditionCompositing”)方法,它可以在彼此内部生成图片集。

现在我试图将两张图片并排放在垂直轴上。

使用UIImage的iphone IOS存在多个示例,这些示例不再适用。有没有人为Maci for NSimage提供一个例子?

这是我的CIAdditionCompositing的代码:

    //------- combining images ----
    // get images from internet
    // webaddress
    let filePath2 = "https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201701131218"
    let filePath1 = "https://staticdelivery.nexusmods.com/mods/110/images/74627-0-1459502036.jpg"
    //create url paths
    let url1 = NSURL(string: filePath1)
    let url2 = NSURL(string: filePath2)
    //retrieve images
    let image1 = NSImage(byReferencing: url1! as URL)
    let image2 = NSImage(byReferencing: url2! as URL)
    // convert images
    //read image 1
    let imageData1 = image1.tiffRepresentation!
    let ciImgage1 = CIImage(data: imageData1)
    //read image 2
    let imageData2 = image2.tiffRepresentation!
    let ciImgage2 = CIImage(data: imageData2)

    // filtersetting
    let filter = CIFilter(name: "CIAdditionCompositing")!
    filter.setDefaults()
    filter.setValue(ciImgage2, forKey: "inputImage")
    filter.setValue(ciImgage1, forKey: "inputBackgroundImage")
    // collect result
    let resultCIImage = filter.outputImage
    // transfer back to nsimage
    let rep = NSCIImageRep(ciImage: resultCIImage!)
    let finalResult = NSImage(size: rep.size)
    finalResult.addRepresentation(rep)

    // display result image....
    MyImage.image = finalResult

0 个答案:

没有答案