LoginSignup
3
3

More than 5 years have passed since last update.

image filter template w/ swift

Last updated at Posted at 2014-09-09
//
//  TopViewController.swift
//
//  Created by Yusuke Sugomori on 9/9/14.
//  Copyright (c) 2014 Yusuke Sugomori. All rights reserved.
//

import UIKit

class TopViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        let screenSize: CGRect = UIScreen.mainScreen().bounds

        let screenWidth = screenSize.width
        let screenHeight = screenSize.height

        let image = UIImage(named: "test.jpg")

        // Core Image context
        let ciContext = CIContext(options: nil)

        // CIImage, think of a CIImage as image data for processing, nothing is displayed or can be displayed at this point
        let coreImage = CIImage(image: image)

        // pick the filter
        let filter = CIFilter(name: "CIPhotoEffectTransfer")

        // pass image as input
        filter.setValue(coreImage, forKey: kCIInputImageKey)

        // retrieve the processed image
        let filteredImageData = filter.valueForKey(kCIOutputImageKey) as CIImage

        // returns a Quartz image from the Core Image context
        let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent())

        // final UIImage ready to be displayed
        let filteredImage = UIImage(CGImage: filteredImageRef)


        let imageView = UIImageView(image: filteredImage)

        imageView.frame = CGRectMake(0, 0, screenWidth, screenHeight)
        imageView.contentMode = UIViewContentMode.ScaleAspectFit

        self.view.addSubview(imageView)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3