I'm using CGContext.init(withUrl) to create a pdf context and doing some rendering inside that context. I was able to save the pdf in the location which will be available in the url. But in my case i will not have a location while creating the CGContext. So in my case i need a create a CGPDFContext without the location. I tried using the CGDataConsumer, but that also not working.
My Requirements
1. Have to create PDF file without the save location.
2. In certain time interval i have to save that pdf to the particular location
Post
Replies
Boosts
Views
Activity
I'm trying to create a NSImage using NSBitmapImageRep. But the images are blurred while creating an image. See the code below
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.