Created
December 5, 2014 03:13
-
-
Save irskep/e560be65163efcb04115 to your computer and use it in GitHub Desktop.
Convert RGBA bytes to UIImage in Swift
This file contains hidden or 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
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