Swift-Add a filter effect to a picture (sepia old photo filter, black and white filter)

Source: Internet
Author: User
Tags uikit

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.