Creating a NSImage using NSBitmapImageRep is returning a blurred image

I'm trying to create a NSImage using NSBitmapImageRep. But the images are blurred while creating an image. See the code below

Code Block func getImage() -> NSImage {
    let imgBitMap = NSBitmapImageRep.init(bitmapDataPlanes: nil, pixelsWide: 2000, pixelsHigh: 2000, bitsPerSample: 16, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)
    NSGraphicsContext.saveGraphicsState()
    let context = NSGraphicsContext.init(bitmapImageRep: imgBitMap!)!
    NSGraphicsContext.current = context
    let ctx = NSGraphicsContext.current!.cgContext
    ctx.setFillColor(NSColor.white.cgColor)
    ctx.fill(CGRect(x: 0, y: 0, width: 2000, height: 2000))
    let row = 20, col = 20
    var y = 0
    for i in 0..<row {
      var x = 0
      for j in 0..<col {
        ctx.saveGState()
        ctx.clip(to: CGRect(x: x, y: y, width: 100, height: 100))
        let text = "\(i)\(j)"
        let attributs = [NSAttributedString.Key.font: NSFont.init(name: "Arial", size: 13.3)!, NSAttributedString.Key.foregroundColor: NSColor.red]
        let attrStr = NSAttributedString.init(string: text, attributes: attributs)
        let frameSetter = CTFramesetterCreateWithAttributedString(attrStr)
        let ctFrame = CTFramesetterCreateFrame(frameSetter, CFRange.init(location: 0, length: text.count), CGPath.init(rect: CGRect(x: x, y: y, width: 100, height: 100), transform: nil), nil)
        CTFrameDraw(ctFrame, ctx)
        ctx.restoreGState()
        x += 100
      }
      y += 100
    }
    NSGraphicsContext.restoreGraphicsState()
    let img = NSImage.init()
    img.addRepresentation(imgBitMap!)
    return img
  }


The above method is returning an image, but resulting a blurry image.



Creating a NSImage using NSBitmapImageRep is returning a blurred image
 
 
Q