I have an image view defined as:
@IBOutlet weak var imgView: NSImageView!
Image data which gets written into defined as:
var imageData = [[Int?]](
repeating: [Int?](repeating: nil, count: 256),
count: 240
)
I'm invoking my onDraw method from a timer. The core part of the drawing which takes the longest is:
let image = NSImage(size: NSSize(width: 240, height: 256))
image.lockFocus()
for y in 0..<240 {
for x in 0..<256 {
let n = imageData[y][x]
let scaledY = 255 - y
let c = NSColor(red: CGFloat(((n! & 0xFF000000) >> 24)) / 255.0, green: CGFloat((n! & 0x00FF0000) >> 16) / 255.0, blue: CGFloat((n! & 0x0000FF00) >> 8) / 255.0, alpha: 1.0)
c.drawSwatch(in: NSRect(x: x, y: scaledY, width: 1, height: 1))
}
}
image.unlockFocus()
imgView.image = image
I've tried changing my timer speed but it looks like the drawing part is taking too long. Trying to reach 30 FPS but it ends up skipping frames due to the time it takes to draw the image data.
Commenting out the creation of the NSColor object and the c.drawSwatch it runs perfectly fast. But obviously draws nothing.
So is there a faster way to draw into an NSImage? Sorry I'm fairly new to Swift and Cocoa development.