帮我弄清楚金属api,我想通过mtlView渲染普通图片,但这对我不起作用,这是我的代码,我在做什么错?接下来,我想使用UISlider和CIColorControls以60fps更改此图片的对比度,请说明。
import MetalKit
class ViewController: UIViewController, MTKViewDelegate {
let device = MTLCreateSystemDefaultDevice()
let colorSpace = CGColorSpaceCreateDeviceRGB()
lazy var commandQueue: MTLCommandQueue = {
[unowned self] in
return self.device!.makeCommandQueue()
}()!
lazy var ciContext: CIContext = {
[unowned self] in
return CIContext(mtlDevice: self.device!)
}()
@IBOutlet weak var mtlView: MTKView!
let filter = CIFilter(name: "CIPhotoEffectProcess" )
let contrastFilter = CIFilter(name: "CIColorControls")
var image = CIImage()
@IBOutlet weak var slider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
mtlView.delegate = self
let img = UIImage(named: "picture")
let ciImage = CIImage(image: img!)
image = ciImage!
}
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
}
func draw(in view: MTKView) {
guard let targetTexture = view.currentDrawable?.texture else {
return
}
let commandBuffer = commandQueue.makeCommandBuffer()
let bounds = CGRect(origin: CGPoint.zero, size: view.drawableSize)
ciContext.render(image,
to: targetTexture,
commandBuffer: commandBuffer,
bounds: bounds,
colorSpace: colorSpace)
commandBuffer?.present(view.currentDrawable!)
commandBuffer?.commit()
}
}
答案 0 :(得分:0)
我看不到任何将device
设置为MTKView
的代码。没有MTKView
的{{1}}将返回空的device
。我建议将其添加到drawable
:
viewDidLoad