Skip to content

Instantly share code, notes, and snippets.

View barbulescualex's full-sized avatar

Alex Barbulescu barbulescualex

View GitHub Profile
func draw(in view: MTKView) {
//create command buffer for ciContext to use to encode it's rendering instructions to our GPU
guard let commandBuffer = metalCommandQueue.makeCommandBuffer() else {
return
}
//make sure we actually have a ciImage to work with
guard var ciImage = currentCIImage else {
return
}
func draw(in view: MTKView) {
var r = view.bounds
r.size = view.drawableSize
let scaleX = r.size.height/outputCIImage.extent.size.height
let transform = CGAffineTransform(scaleX: scaleX, y: scaleX)
outputCIImage = outputCIImage.transformed(by: transform)
outputCIImage = outputCIImage.cropped(to: r)
let x = -r.origin.x
let y = -r.origin.y
extension ViewController {
//MARK:- View Setup
func setupView(){
view.backgroundColor = .black
mtkView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(mtkView)
view.addSubview(switchCameraButton)
view.addSubview(captureImageButton)
view.addSubview(capturedImageView)
import UIKit
import AVFoundation
import MetalKit
class ViewController: UIViewController {
...
//MARK:- View Components
func switchCameraInput(){
//don't let user spam the button, fun for the user, not fun for performance
switchCameraButton.isUserInteractionEnabled = false
//reconfigure the input
captureSession.beginConfiguration()
if backCameraOn {
captureSession.removeInput(backInput)
captureSession.addInput(frontInput)
backCameraOn = false
class ViewController: UIViewController {
...
let fadeFilter = CIFilter(name: "CIPhotoEffectFade")
let sepiaFilter = CIFilter(name: "CISepiaTone")
...
//MARK:- Core Image
extension ViewController : MTKViewDelegate {
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
//tells us the drawable's size has changed
}
func draw(in view: MTKView) {
//create command buffer for ciContext to use to encode it's rendering instructions to our GPU
guard let commandBuffer = metalCommandQueue.makeCommandBuffer() else {
return
}
class ViewController: UIViewController {
...
//core image
var currentCIImage : CIImage?
...
//MARK:- Metal
class ViewController: UIViewController {
...
//core image
var ciContext : CIContext!
...
override func viewDidAppear(_ animated: Bool) {
class ViewController: UIViewController {
...
func setupMetal(){
//fetch the default gpu of the device (only one on iOS devices)
metalDevice = MTLCreateSystemDefaultDevice()
//tell our MTKView which gpu to use
mtkView.device = metalDevice