Skip to content

Instantly share code, notes, and snippets.

@ctreffs
ctreffs / MTLARFrameProcessor.swift
Created November 16, 2023 14:08
Processes ARKits' ARFrame->capturedImage CVPixelBuffer according to the documentation into an sRGB image
import CoreImage
import CoreVideo
import ImageIO
import Metal
/// Processes ARKits' ARFrame->capturedImage CVPixelBuffer according to the documentation into an sRGB image.
///
/// ARKit captures pixel buffers in a full-range planar YCbCr format (also known as YUV) format according to the ITU R. 601-4 standard.
/// (You can verify this by checking the kCVImageBufferYCbCrMatrixKey pixel buffer attachment.)
/// Unlike some uses of that standard, ARKit captures full-range color space values, not video-range values.
@lobianco
lobianco / yuv_buffer_to_image.swift
Created June 8, 2020 17:51
Converts a frame from a YUV video sample buffer (like what RPScreenRecorder provides) into a UIImage.
import Accelerate
import CoreGraphics
import CoreMedia
import Foundation
import QuartzCore
import UIKit
func createImage(from sampleBuffer: CMSampleBuffer) -> UIImage? {
guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
return nil