forked from chriseidhof/core-image-video
-
Notifications
You must be signed in to change notification settings - Fork 29
/
CoreImageView.swift
43 lines (37 loc) · 1.25 KB
/
CoreImageView.swift
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
//
// CoreImageView.swift
// CoreImageVideo
//
// Created by Chris Eidhof on 03/04/15.
// Copyright (c) 2015 objc.io. All rights reserved.
//
import Foundation
import GLKit
class CoreImageView: GLKView {
var image: CIImage? {
didSet {
display()
}
}
let coreImageContext: CIContext
override convenience init(frame: CGRect) {
let eaglContext = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
self.init(frame: frame, context: eaglContext)
}
override init(frame: CGRect, context eaglContext: EAGLContext!) {
coreImageContext = CIContext(EAGLContext: eaglContext)
super.init(frame: frame, context: eaglContext)
// We will be calling display() directly, hence this needs to be false
enableSetNeedsDisplay = false
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawRect(rect: CGRect) {
if let img = image {
let scale = self.window?.screen.scale ?? 1.0
let destRect = CGRectApplyAffineTransform(bounds, CGAffineTransformMakeScale(scale, scale))
coreImageContext.drawImage(img, inRect: destRect, fromRect: img.extent())
}
}
}