Skip to content

Instantly share code, notes, and snippets.

@guzhenhuaGitHub
Created May 21, 2021 03:37
Show Gist options
  • Save guzhenhuaGitHub/90cdb41d017b939d50ac220c0aa0dc0a to your computer and use it in GitHub Desktop.
Save guzhenhuaGitHub/90cdb41d017b939d50ac220c0aa0dc0a to your computer and use it in GitHub Desktop.
图像的绘制通常是指用那些以 CG 开头的方法把图像绘制到画布中,然后从画布创建图片并显示这样一个过程。这个最常见的地方就是 [UIView drawRect:] 里面了。由于 CoreGraphic 方法通常都是线程安全的,所以图像的绘制可以很容易的放到后台线程进行。
public extension CGImage {
func bitmapImage() -> CGImage? {
guard
let colorSpace = colorSpace,
let context = CGContext(
data: nil,
width: width,
height: height,
bitsPerComponent: bitsPerComponent,
bytesPerRow:bytesPerRow,
space: colorSpace,
bitmapInfo: bitmapInfo.rawValue)
else {
return nil
}
context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height))
return context.makeImage()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment