Sure, create a blank project and make this your viewcontroller code. If it works, you'll see a rainbow gradient on your screen. I can verify this works on my iPhone XS
//
// ViewController.swift
// TestBuffer
//
// Created by Ilardi, Michael on 4/9/21.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//create a rainbow RGB in a Uint32 array
let width = 256,height=256
var buffer: [UInt32] = []
for i in 0...width - 1 {
for j in 0...height - 1{
var color:UInt32
let r = UInt32 ((Float(i)/(Float(height))) * 255.0)
let g = UInt32 ((Float(j)/(Float(width))) * 255.0)
let b = UInt32 (128)
color = (r 24) + (g 16) + (b 8) + 255;
buffer.append(color)
}
}
//now create a CVPixelBuffer and copy our rainbow array into it
let format = kCVPixelFormatType_32ARGB
// kCVPixelFormatType_32ARGB
let sourcePixelBufferOptions: NSDictionary = [kCVPixelBufferPixelFormatTypeKey: format,
kCVPixelBufferWidthKey: width,
kCVPixelBufferHeightKey: height,
kCVPixelBufferIOSurfacePropertiesKey: NSDictionary(),
kCVPixelBufferIOSurfaceCoreAnimationCompatibilityKey:true,
kCVPixelBufferCGBitmapContextCompatibilityKey:true
]
var pix:CVPixelBuffer?
CVPixelBufferCreate(kCFAllocatorDefault, width, height, format, sourcePixelBufferOptions, &pix)
let flags = CVPixelBufferLockFlags(rawValue: 0)
CVPixelBufferLockBaseAddress(pix! ,flags)
let pixelBufferAddress = CVPixelBufferGetBaseAddress((pix)!)
memcpy(pixelBufferAddress, buffer, 4 * height * width)
CVPixelBufferUnlockBaseAddress(pix!, flags)
//set the main view's layer contents to our pixelbuffer
view.layer.contents = pix
}
}