-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLooksBrowserViewController.swift
197 lines (154 loc) · 6.89 KB
/
LooksBrowserViewController.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// LooksBrowserViewController.swift
// MovieLooks
//
// Created by Sean Hess on 5/6/16.
//
//
import UIKit
import BButton
import Crashlytics
let LookCellIdentifier = "LookCell"
let LookGroupHeaderIdentifier = "LookGroupHeader"
class LooksBrowserViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var nextButton: BButton!
@IBOutlet weak var looksView: UICollectionView!
var keyFrame : UIImage!
var renderer : ES2Renderer!
var cellSize : CGSize = CGSize(width: 170, height: 170)
var selectedLook : Look?
var videoURL: URL?
var videoMode = VideoModeWideSceenLandscape
let lookGroups = PurchaseManager.sharedManager.looks
var lookStates : [Look : LookCellState] = [:]
override func viewDidLoad() {
super.viewDidLoad()
nextButton.setType(.primary)
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: nil, action: nil)
if (videoURL == nil) {
CLSLogv("loadVideo not called before load", getVaList([]))
Crashlytics.sharedInstance().crash()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
override func didReceiveMemoryWarning() {
// lookStates = [:]
super.didReceiveMemoryWarning()
}
// needs to be called BEFORE loading
func loadVideo(_ videoURL:URL) throws {
self.videoURL = videoURL
keyFrame = try Video.sharedManager.keyFrame(videoURL, atTime: kCMTimeZero)
cellSize = cellSize(keyFrame)
lookStates = lookStates(lookGroups)
let scale = UIScreen.main.scale
let outputSize = CGSize(width: cellSize.width * scale, height: cellSize.height * scale)
renderer = ES2Renderer(frameSize: outputSize, outputFrameSize: outputSize)
startRender(keyFrame)
}
func startRender(_ keyFrame:UIImage) {
// no matter what you put in for the size, the renderer will create a square image and fill the space
// that you give it. So make sure this is square
let scale = UIScreen.main.scale
let outputSize = CGSize(width: cellSize.width * scale, height: cellSize.height * scale)
renderer.resetFrameSize(outputSize, outputFrameSize: outputSize)
renderer.resetRenderBuffer()
renderer.loadKeyFrame(keyFrame)
DispatchQueue.global().async {
self.renderLoop()
}
}
func lookStates(_ lookGroups: [LookGroup]) -> [Look : LookCellState] {
var states : [Look : LookCellState] = [:]
lookGroups
.flatMap({group in
return group.items.map({look in
return LookCellState(look: look)
})
})
.forEach({(state : LookCellState) in
states[state.look] = state
})
return states
}
func renderLoop() {
// create all the look states
var renderStates = Array(lookStates.values)
// NOTE: the first one fails for some reason (this was there in the legacy code uncommented)
// so render it twice
renderStates = renderStates + [renderStates[0]]
renderStates.forEach { (state) in
let look = state.look
renderer.loadLookParam(look.data, with: self.videoMode)
renderer.looksStrengthValue = 1.0
renderer.looksBrightnessValue = 0.5
let processedCGImageRef = renderer.frameProcessingAndReturnImage(nil, flipPixel:false)
// if(videoMode==VideoModeWideSceenPortrait || videoMode==VideoModeTraditionalPortrait) {
// processedImage = [[UIImage alloc] initWithCGImage:processedCGImageRef scale:1.0 orientation:UIImageOrientationRight];
// }
// else {
// processedImage = [[UIImage alloc] initWithCGImage:processedCGImageRef];
// }
let processedImage = UIImage(cgImage: (processedCGImageRef?.takeUnretainedValue())!)
print(" - got image", look.name)
DispatchQueue.main.async {
print(" - set image", look.name)
state.image = processedImage
state.onRender(processedImage)
}
}
}
func cellSize(_ keyFrame:UIImage) -> CGSize {
// these should always be square
return CGSize(width: 170, height: 170)
}
@IBAction func tappedNext() {
print("NEXT")
self.performSegue(withIdentifier: "LookPreviewController", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let preview = segue.destination as? LookPreviewController {
preview.videoURL = videoURL
preview.look = selectedLook
preview.keyFrame = keyFrame
preview.videoMode = self.videoMode
preview.renderer = self.renderer
}
}
//// Collection View //////////////////////////////////////////////////////
func numberOfSections(in collectionView: UICollectionView) -> Int {
return lookGroups.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let group = lookGroups[section]
return group.items.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let group = lookGroups[indexPath.section]
selectedLook = group.items[indexPath.item]
nextButton.isEnabled = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LookCellIdentifier, for: indexPath) as! LookCell
let group = lookGroups[indexPath.section]
let look = group.items[indexPath.item]
cell.label.text = look.name
if let state = lookStates[look] {
print("Got cell", look.name)
cell.update(state)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return cellSize
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: LookGroupHeaderIdentifier, for: indexPath) as! LookGroupHeader
let group = lookGroups[indexPath.section]
header.label.text = group.name
return header
}
}