Help us understand the problem. What is going on with this article?

Swift:左右反転した画像,白黒画像,ネガポジ反転画像を生成する

NSImageにはサイズとピクセルサイズの概念がある(これはRetinaディスプレイ5Kディスプレイへの対応のため)が,NSImageに対してそのままアフィン変換を適用したら,画像のピクセルサイズがおかしくなってしまった.これを避ける方法を色々試した結果,一旦CIFilterを咬ませる方法が良さそうだとわかった.

フィルタのソース
extension NSImage {
    public var ciImage: CIImage? {
        guard let imageData = self.tiffRepresentation else { return nil }
        return CIImage(data: imageData)
    }

    public var flipped: NSImage {
        guard let flipFilter = CIFilter(name: "CIAffineTransform") else { return self }
        flipFilter.setValue(self.ciImage, forKey: kCIInputImageKey)
        flipFilter.setValue(CGAffineTransform(a: -1, b: 0, c: 0, d: 1, tx: self.size.width, ty: 0), forKey: kCIInputTransformKey)
        guard let output = flipFilter.outputImage else { return self }
        let rep = NSCIImageRep(ciImage: output)
        let nsImage = NSImage(size: rep.size)
        nsImage.addRepresentation(rep)
        return nsImage
    }

    public var mono: NSImage {
        guard let monoFilter = CIFilter(name: "CIPhotoEffectMono") else { return self }
        monoFilter.setValue(self.ciImage, forKey: kCIInputImageKey)
        guard let output = monoFilter.outputImage else { return self }
        let rep = NSCIImageRep(ciImage: output)
        let nsImage = NSImage(size: rep.size)
        nsImage.addRepresentation(rep)
        return nsImage
    }

    public var inverted: NSImage {
        guard let invertFilter = CIFilter(name: "CIColorInvert") else { return self }
        invertFilter.setValue(self.ciImage, forKey: kCIInputImageKey)
        guard let output = invertFilter.outputImage else { return self }
        let rep = NSCIImageRep(ciImage: output)
        let nsImage = NSImage(size: rep.size)
        nsImage.addRepresentation(rep)
        return nsImage
    }
}
使用例
let original = NSImage(imageLiteralResourceName: "Sample")

let flippedImage = original.flipped
let monoImage = original.mono
let invertedImage = original.inverted
Kyome
趣味でmacOS アプリを開発しています RunCatを作った人です 可愛いは正義
https://kyome.io
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした