This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import AVFoundation | |
| import MetalKit | |
| class ViewController: UIViewController { | |
| ... | |
| //MARK:- View Components | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ViewController: UIViewController { | |
| ... | |
| let fadeFilter = CIFilter(name: "CIPhotoEffectFade") | |
| let sepiaFilter = CIFilter(name: "CISepiaTone") | |
| ... | |
| //MARK:- Core Image |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ViewController: UIViewController { | |
| ... | |
| //core image | |
| var currentCIImage : CIImage? | |
| ... | |
| //MARK:- Metal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ViewController: UIViewController { | |
| ... | |
| //core image | |
| var ciContext : CIContext! | |
| ... | |
| override func viewDidAppear(_ animated: Bool) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
NewerOlder