Closed thickfive closed 6 years ago
I tried to get an image from an UInt8 arrary of RGBA, but i didn't get any useful method of how to covert it, until found soluton in your code.
Here is my code in swift3.0, hope that can help anyone else who meets the same problem.
Thanks!
func imageRefFromBitmap() { // CFData let size: (width: Int, height: Int) = (200, 150) let bitmapData = CFDataCreateMutable(nil, 0) CFDataSetLength(bitmapData, size.width * size.height * 4) // BytePtr let bitmap = CFDataGetMutableBytePtr(bitmapData) // RGBA, 0~255 for i in 0..<size.height { for j in 0..<size.width { let k = 4 * (j + i * size.height) bitmap?[k+0] = 255 // red bitmap?[k+1] = 0 // green bitmap?[k+2] = 0 // blue bitmap?[k+3] = 255 // alpha } } // CGImage let provider = CGDataProvider.init(data: bitmapData!) let imageRef = CGImage.init(width: size.width, height: size.height, bitsPerComponent: 8, bitsPerPixel: 4 * 8, bytesPerRow: 4 * size.width, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: [CGBitmapInfo.byteOrderMask, CGBitmapInfo.init(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)], provider: provider!, decode: nil, shouldInterpolate: false, intent: CGColorRenderingIntent.defaultIntent) // In my case, self is an instance of UIView, it works fine layer.contents = imageRef }
I tried to get an image from an UInt8 arrary of RGBA, but i didn't get any useful method of how to covert it, until found soluton in your code.
Here is my code in swift3.0, hope that can help anyone else who meets the same problem.
Thanks!