效果图
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| import UIKit
import CoreImage
class ViewController: UIViewController {
let myInputImage = CIImage(image: UIImage(named: "test.png"))
var myImageView: UIImageView!
let myButton: UIButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
myImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
myImageView.image = UIImage(CIImage: myInputImage)
self.view.addSubview(myImageView)
myButton.frame = CGRectMake(0,0,80,80)
myButton.backgroundColor = UIColor.grayColor();
myButton.layer.masksToBounds = true
myButton.setTitle("Click", forState: UIControlState.Normal)
myButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
myButton.layer.cornerRadius = 40.0
myButton.layer.position = CGPoint(x: self.view.frame.width/2, y:self.view.frame.height - 50)
myButton.tag = 1
myButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside)
self.view.backgroundColor = UIColor.blackColor()
self.view.addSubview(myButton);
}
func onClickMyButton(sender: UIButton){
let myFilter = CIFilter(name: "CISepiaTone")
myFilter.setValue(myInputImage, forKey: kCIInputImageKey)
myFilter.setValue(1.0, forKey: kCIInputIntensityKey)
var myOutputImage : CIImage = myFilter.outputImage
myImageView.image = UIImage(CIImage: myOutputImage)
myImageView.setNeedsDisplay()
}
}
|