Skip to content

Instantly share code, notes, and snippets.

@matachi
Created November 12, 2018 08:21
Show Gist options
  • Save matachi/098c016fc1bf52016992e894e523a4b6 to your computer and use it in GitHub Desktop.
Save matachi/098c016fc1bf52016992e894e523a4b6 to your computer and use it in GitHub Desktop.
Make an image from a byte array in Swift.
class BytesToTexture {
func texture(bytes: [UInt8], bytesPerRow: Int) -> CGImage {
let rgbaData = CFDataCreate(nil, bytes, bytes.count)!
let provider = CGDataProvider(data: rgbaData)!
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue)
return CGImage(
width: bytesPerRow / 4,
height: bytes.count / bytesPerRow,
bitsPerComponent: 8,
bitsPerPixel: 32,
bytesPerRow: bytesPerRow,
space: colorSpace,
bitmapInfo: bitmapInfo,
provider: provider,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent
)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment