Playground CGContext nil

Hi!

When run below code in iOS blank Playground

Code Block
import UIKit
let contextBig = CGContext(
    data: nil,
    width: 560,
    height: 560,
    bitsPerComponent: 8,
    bytesPerRow: 2240,
    space: CGColorSpace(name: CGColorSpace.sRGB)!,
    bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)
print (contextBig as Any)


Result in console:

Code Block
Optional(<CGContext 0x600003cc0540> (kCGContextTypeBitmap)
<<CGColorSpace 0x600002ddc300> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1)>
width = 560, height = 560, bpc = 8, bpp = 32, row bytes = 2240 
kCGImageAlphaPremultipliedFirst | 0 (default byte order) | kCGImagePixelFormatPacked (default) )


But when change width and height to 561, result in console:

Code Block
nil

Is there for playground some memory limits or etc?

Xcode - Version 12.3 (12C33)

Accepted Reply

Is there for playground some memory limits or etc?

Not clearly documented, but there might have been.

As far as I tried your code on Xcode Playground of 12.4, it showed non nil result.
The issue may be fixed in 12.4, or some other things may be affecting.

Replies

Is there for playground some memory limits or etc?

Not clearly documented, but there might have been.

As far as I tried your code on Xcode Playground of 12.4, it showed non nil result.
The issue may be fixed in 12.4, or some other things may be affecting.
OOPer , thank you for your results.

I upgraded Xcode to 12.4, but no luck.
With params 560 - not nil, with 561 - nil.

My system configuration is

MacBook Air (Retina, 13-inch, 2020), 16GB RAM, i5
macOS Catalina 10.15.7 (19H524)
Xcode Version 12.4 (12D4e)


Also I tried set env variables CGBITMAP_CONTEXT_LOG_ERRORS, CG_CONTEXT_SHOW_BACKTRACE, but I don't have additional info in console.

macOS Catalina 10.15.7 (19H524)

Thanks for clarifying. I tried on Mac mini 2018 with macOS Big Sur 11.1.
Quite interesting difference.Not sure any of the settings may be affecting or not.
OOPer , thank you for your help!

I runed code in blank test iOS app and see in console

Code Block
2021-02-27 13:15:29.587193+0300 TestApp[2104:187450] [Unknown process name] CGBitmapContextCreate: invalid data bytes/row: should be at least 2244 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedFirst.
nil



So my mistake is use bytesPerRow: 2240 as constant, but increase width. In my original playground I use bytesPerRow from old image, but only increase image(context)size for new image.

Correct using CGContext with data = nil, you can pass bytesPerRow: 0 for auto calc.

It's sorry of course that Playground don't show all message in console like simulator.
Thanks for revealing the fact you found. In fact, I was just changing height while trying (although you clearly described change width and height in your original post). Will try to help better if I have next chance.