Created
October 6, 2018 04:03
-
-
Save kunofellasleep/93aca5deabedc5595dcab2e6e5a5983b to your computer and use it in GitHub Desktop.
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 image(from mtlTexture: MTLTexture) -> UIImage { | |
//画像サイズ | |
let w = mtlTexture.width | |
let h = mtlTexture.height | |
let bytesPerPixel: Int = 4 | |
let imageByteCount = w * h * bytesPerPixel | |
let bytesPerRow = w * bytesPerPixel | |
var src = [UInt8](repeating: 0, count: Int(imageByteCount)) | |
//CGImageに変換 | |
let region = MTLRegionMake2D(0, 0, w, h) | |
mtlTexture.getBytes(&src, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0) | |
let bitmapInfo = CGBitmapInfo(rawValue: (CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue)) | |
let colorSpace = CGColorSpaceCreateDeviceRGB() | |
let bitsPerComponent = 8 | |
let context = CGContext(data: &src, | |
width: w, | |
height: h, | |
bitsPerComponent: bitsPerComponent, | |
bytesPerRow: bytesPerRow, | |
space: colorSpace, | |
bitmapInfo: bitmapInfo.rawValue) | |
let cgImage = context?.makeImage() | |
//UIImageに変換して返す | |
let image = UIImage(cgImage: cgImage!) | |
return image | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment