Unable to create simple CGContext in Playground. What am I doing wrong?

I am trying to create a simple bitmap image. I was able to create one using an IOS-based (UIKit) playground but I am unable to to do it in a MacOs (Quartz) framework. I think those are the right terms ... I'm a newbee at this!

My code, below, is needlessly verbose but I wanted to understand every part. The key is the CGContext call at the bottom, for every iteration it has returned NULL and fails.

I have tried it with the current data: nil and supplying a data: &Data reference for the image. Both attempts fail.

I must be doing (or assuming) something incorrectly.

Here's the code:

import PlaygroundSupport
import Cocoa
import CoreGraphics

let tftWidthPixels = 240

let tftHeightPixels = 135

let tftSize = CGSize(width: tftWidthPixels, height: tftHeightPixels)

// Border for guideline extensions

let topBorderPixels = 200

let leftBorderPixels = 200

let pixelSizeWidth = 10  // This seems to work well

let pixelSizeHeight = pixelSizeWidth  // Square

let pixelSize = CGSize(width: pixelSizeWidth, height: pixelSizeHeight)

// This should (must?) be odd

let lineWidth = 3

// We outline every tft pixel

let imageVerticalLines = tftWidthPixels + 1

let imageHorizontalLines = tftHeightPixels + 1

let imageHeightPixels = tftHeightPixels * pixelSizeHeight + topBorderPixels

let imageWidthPixels = tftWidthPixels * pixelSizeWidth + leftBorderPixels

let bitsPerByte = 8

let bytesPerPixel = 4

let bitsPerPixel = bytesPerPixel * bitsPerByte

let bytesPerRow = bytesPerPixel * imageWidthPixels

let imageSize = CGSize(width: imageWidthPixels, height: imageHeightPixels)

let imageSizeBytes = imageWidthPixels * imageHeightPixels * bytesPerPixel

let startRow = (lineWidth % 2 + Int(lineWidth/2) - 1) + topBorderPixels // Allow for pixel overlap

let startColumn = (lineWidth % 2 + Int(lineWidth/2) - 1) + leftBorderPixels

let lineColorNormal = CGColor(gray: 0.5, alpha: 1.0)

let lineColorDark = CGColor(gray: 0.25, alpha: 1.0)

let borderLineColor = CGColor(gray: 0.1, alpha: 1.0)

let pageFillColor = CGColor.white

let imageFormat = CIFormat.ARGB8

let imageColorSpace = CGColorSpace(name: CGColorSpace.sRGB)!

let imageBitMapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue)

let imageIntent = CGColorRenderingIntent.defaultIntent

//  Basically this is the cell size to draw, every n lines will be lineColorDark

let majorGridVertical = 10

let majorGridHorizontal = 10

let bitmapInfo = CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.none.rawValue

print("Byte order mask=\(CGBitmapInfo.byteOrderMask)")

let imageCount = imageHeightPixels*imageWidthPixels*bitsPerPixel/bitsPerByte

print("Image count=\(imageCount)")

print("Context parameters: width=\(imageWidthPixels), height=\(imageHeightPixels), bytesPerRow=\(bytesPerRow), \ncolorSpace=\(imageColorSpace), \nbitMapInfo=\(bitmapInfo)")

guard var graphContext = CGContext(data: nil, width: imageWidthPixels, height: imageHeightPixels, bitsPerComponent: bitsPerByte, bytesPerRow: 0, space: imageColorSpace, bitmapInfo: bitmapInfo) else {

    fatalError("Unable to create the CGContext")

}



// All is well, so far. What a wonder!```

Accepted Reply

SOLVED!

Apparently CGImageAlphaInfo.none.rawValue Isn’t supported! Use one of the other two options …

Replies

SOLVED!

Apparently CGImageAlphaInfo.none.rawValue Isn’t supported! Use one of the other two options …