As you know, dngPhotoDataRepresentation() API has deprecated at iOS 11 and later. So, We need to change the API to the fileDataRepresentation(). But How?

Almost codes are same previous. Here it is.

Initial:

	override func viewDidLoad() {
		super.viewDidLoad()

		self.captureSession = AVCaptureSession()
		self.captureSession?.sessionPreset = .photo
		self.capturePhotoOutput = AVCapturePhotoOutput()
                self.captureDevice = AVCaptureDevice.default(for: .video)
                let input = try! AVCaptureDeviceInput(device: self.captureDevice!)
                self.captureSession?.addInput(input)
                self.captureSession?.addOutput(self.capturePhotoOutput!)

		self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession!)
		self.previewLayer?.frame = self.preView.bounds
		self.previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
		self.previewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
		self.preView.layer.addSublayer(self.previewLayer!)
		self.captureSession?.startRunning()
	}

Capture:

	@IBAction func capture(_ sender: Any) {
		let photoSettings : AVCapturePhotoSettings!
		guard let availableRawFormat = capturePhotoOutput?.__availableRawPhotoPixelFormatTypes.first else { return }
		photoSettings = AVCapturePhotoSettings(rawPixelFormatType: availableRawFormat.uint32Value)
		photoSettings.isAutoStillImageStabilizationEnabled = false
		photoSettings.flashMode = .off
		photoSettings.isHighResolutionPhotoEnabled = false
		let desiredPreviewPixelFormat = NSNumber(value: kCVPixelFormatType_32BGRA)
		if photoSettings.__availablePreviewPhotoPixelFormatTypes.contains(desiredPreviewPixelFormat) {
			photoSettings.previewPhotoFormat = [
				kCVPixelBufferPixelFormatTypeKey as String : desiredPreviewPixelFormat,
				kCVPixelBufferWidthKey as String : NSNumber(value: 512),
				kCVPixelBufferHeightKey as String : NSNumber(value: 512)
			]
		}
		self.capturePhotoOutput?.capturePhoto(with: photoSettings, delegate: self)
	}

Store:

	func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

		let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
		let formatter = DateFormatter()
		formatter.dateFormat = "yyyyMMddHHmmss"
		formatter.locale = Locale.init(identifier: "en_US_POSIX")
		let filePath =  dir.appending(String(format: "/%@.dng", formatter.string(from: Date())))
		let dngFileURL = URL(fileURLWithPath: filePath)

		let dngData = photo.fileDataRepresentation()!
		do {
			try dngData.write(to: dngFileURL, options: [])
		} catch {
			print("Unable to write DNG file.")
			return
		}

		let items = [dngFileURL]
		let activityView = UIActivityViewController.init(activityItems: items, applicationActivities: nil)
		activityView.popoverPresentationController?.sourceView = self.view
		activityView.excludedActivityTypes = [
			UIActivityType.copyToPasteboard,
			UIActivityType.assignToContact,
			UIActivityType.openInIBooks,
		]
		self.present(activityView, animated: true, completion: nil)
	}

* NOTICE: DNG file can’t store into Photo Library. 

Usage: touch the “Capture” button to capture, then Activity view will be shown. That’s it.

 

Hope this will help you 😗 Project code is on GitHub.