Created
November 12, 2018 08:21
-
-
Save matachi/098c016fc1bf52016992e894e523a4b6 to your computer and use it in GitHub Desktop.
Make an image from a byte array in Swift.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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