Last active
August 29, 2015 14:19
-
-
Save el-hoshino/3b39428e760a7c7ac3db to your computer and use it in GitHub Desktop.
【質問】UIKit のみで OnScreen 描画フィルター掛けることできないかな? ref: http://qiita.com/lovee/items/3e3b6233df9839053521
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import UIKit | |
// オリジナル画像を読み込もう(ファイル名の設定忘れないでね) | |
let baseImage = UIImage(named: "image.png")! | |
let baseCIImage = CIImage(image: baseImage) | |
// Core Image フィルターを #00FFFF の色で設定する | |
let filterColor = CIColor(red: 0, green: 1, blue: 1) | |
let colorGenerator = CIFilter(name: "CIConstantColorGenerator") | |
colorGenerator.setValue(filterColor, forKey: "inputColor") | |
let filterImage = colorGenerator.valueForKey("outputImage") as! CIImage | |
// オリジナル画像に先ほど設定した色で乗算で掛けましょう | |
let multiplier = CIFilter(name: "CIMultiplyCompositing") | |
multiplier.setValue(filterImage, forKey:"inputImage") | |
multiplier.setValue(baseCIImage, forKey:"inputBackgroundImage") | |
let filteredCIImage = multiplier.valueForKey("outputImage") as! CIImage | |
// 結果画像 | |
let filteredImage = UIImage(CIImage: filteredCIImage)! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import UIKit | |
// オリジナル画像を読み込もう(ファイル名の設定忘れないでね) | |
let baseImage = UIImage(named: "image.png")! | |
// オフスクリーン描画の初期化設定 | |
UIGraphicsBeginImageContext(baseImage.size) | |
let ctx = UIGraphicsGetCurrentContext() | |
let rect = CGRect(origin: CGPointZero, size: baseImage.size) | |
// Core Graphics と UIImage は座標系違うので変換しておく | |
CGContextTranslateCTM(ctx, 0, baseImage.size.height) | |
CGContextScaleCTM(ctx, 1.0, -1.0) | |
// 描画モードを Multiply に設定しておく | |
CGContextSetBlendMode(ctx, kCGBlendModeMultiply) | |
// 元画像をオフスクリーンキャンバスで描く | |
CGContextDrawImage(ctx, rect, baseImage.CGImage) | |
// 元画像をクリッピングマスクとして描画領域を設定する | |
CGContextClipToMask(ctx, rect, baseImage.CGImage) | |
CGContextAddRect(ctx, rect) | |
// フィルターを設定する | |
let filterColor = UIColor(red: 0, green: 1, blue: 1, alpha: 1) | |
filterColor.setFill() | |
CGContextDrawPath(ctx, kCGPathFill) | |
// オフスクリーン描画結果を UIImage として出力する | |
let filteredImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment