Core Image is a powerful filter processing framework. In addition to adding various built-in filters directly to the image, it can also accurately modify the vividness, color, exposure, etc. The following two examples demonstrate how to add filters to UIImage.
1. Sepia filter-CISepiaTone
This is a retro yellow photo effect (the above picture is the original picture).
technology sharing
Expand the UIImage class and add a sepia filter:
import UIKit
// --- UIImageFilterExtension.swift ---
extension UIImage
{
// Sepia retro filter (old photo effect)
func sepiaTone ()-> UIImage?
{
let imageData = UIImagePNGRepresentation (self)
let inputImage = CoreImage.CIImage (data: imageData!)
let context = CIContext (options: nil)
let filter = CIFilter (name: "CISepiaTone")
filter! .setValue (inputImage, forKey: kCIInputImageKey)
filter! .setValue (0.8, forKey: "inputIntensity")
if let outputImage = filter! .outputImage {
let outImage = context.createCGImage (outputImage, fromRect: outputImage.extent)
return UIImage (CGImage: outImage)
}
return nil
}
}
Example of use:
1
imageView1.image = UIImage (named: "img2.jpg") ?. sepiaTone ()
2. Black and white filter-CIPhotoEffectNoir
technology sharing
Expand the UIImage class and add black and white filters:
import UIKit
// --- UIImageFilterExtension.swift ---
extension UIImage
{
// Black and white effect filter
func noir ()-> UIImage?
{
let imageData = UIImagePNGRepresentation (self)
let inputImage = CoreImage.CIImage (data: imageData!)
let context = CIContext (options: nil)
let filter = CIFilter (name: "CIPhotoEffectNoir")
filter! .setValue (inputImage, forKey: kCIInputImageKey)
if let outputImage = filter! .outputImage {
let outImage = context.createCGImage (outputImage, fromRect: outputImage.extent)
return UIImage (CGImage: outImage)
}
return nil
}
}
Example of use:
1
imageView1.image = UIImage (named: "img2.jpg") ?. noir ()
Original text from: www.hangge.com Reprint please keep the original text link: http://www.hangge.com/blog/cache/detail_889.html
Swift-Add filter effects to pictures (sepia old photo filter, black and white filter)
label:
Original address: http://www.cnblogs.com/Free-Thinker/p/4858382.html