Cannot save CoreML generated image to tempDirectory

I created a style transfer model using CreateML and can not save the generated styled image to tempDirectory, unsure if it is to do with the way I create the pixelBuffer? (below):

import Vision
import CoreML
import CoreVideo

      let model = style1()

      // set input size of the model
      var modelInputSize = CGSize(width: 512, height: 512)
      // create a cvpixel buffer
      var pixelBuffer: CVPixelBuffer?
      let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
                   kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary
      CVPixelBufferCreate(kCFAllocatorDefault,
                          Int(modelInputSize.width),
                          Int(modelInputSize.height),
                          kCVPixelFormatType_32BGRA,
                          attrs,
                          &pixelBuffer)

      // put bytes into pixelBuffer
      let context = CIContext()
      let argPathUrl = "file:///pathhere"
      let modelImageUrl: URL = URL(string: argPathUrl)!;
      guard let CiImageData = CIImage(contentsOf: modelImageUrl) else { return }
      context.render(CiImageData, to: pixelBuffer!)

      // predict image
      let output = try? model.prediction(image: pixelBuffer!)
      let predImage = CIImage(cvPixelBuffer: (output?.stylizedImage)!)
      let context2 = CIContext()
      let format = kCIFormatRGBA16
      
      try! context2.writePNGRepresentation(of: predImage, to: FileManager.default.temporaryDirectory.appendingPathComponent("testcgi.png"), format: format, colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!, options: [:])
      
      let saveUrl = "testcgi.png"

      return;