Skip to content

Instantly share code, notes, and snippets.

@irskep
Created December 5, 2014 03:13
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save irskep/e560be65163efcb04115 to your computer and use it in GitHub Desktop.
Save irskep/e560be65163efcb04115 to your computer and use it in GitHub Desktop.
Convert RGBA bytes to UIImage in Swift
func getUIImageForRGBAData(#width: UInt, #height: UInt, #data: NSData) -> UIImage? {
let pixelData = data.bytes
let bytesPerPixel:UInt = 4
let scanWidth = bytesPerPixel * width
let provider = CGDataProviderCreateWithData(nil, pixelData, height * scanWidth, nil)
let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
var bitmapInfo:CGBitmapInfo = .ByteOrderDefault;
bitmapInfo |= CGBitmapInfo(CGImageAlphaInfo.Last.rawValue)
let renderingIntent = kCGRenderingIntentDefault;
let imageRef = CGImageCreate(width, height, 8, bytesPerPixel * 8, scanWidth, colorSpaceRef,
bitmapInfo, provider, nil, false, renderingIntent);
return UIImage(CGImage: imageRef)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment