"IOSurface creation failed" drawing to CGContext

(more details on StackOverflow)

I'm getting messages like the following, SOMETIMES, when I draw to a CGContext

IOSurface creation failed: e00002c2 parentID: 00000000 properties: {
    IOSurfaceAddress = 5207703552;
    IOSurfaceAllocSize = 9461418;
    IOSurfaceCacheMode = 0;
    IOSurfaceName = CMPhoto;
    IOSurfacePixelFormat = 1246774599;
}

call to context.draw():

context.draw(photo.image,
                     in: CGRect(x: 0, y: top, width: width, height: height),
                     byTiling: false)

The results are just fine, so the draw seems to be working. It also, most often, draws without producing this error, but it fails pretty often.

I'm not sure where to begin looking to sort out what I might need to do differently to avoid this error message in the console.


Complete code:

import Foundation
import SwiftUI

func generateSpritesImage(thumbPhotos: [Photo], width: Int, filename: URL) -> [Int] {
    var indices = [Int]()
    let totalHeight = thumbPhotos.reduce(0) {
        $0 + $1.heightOfImage(ofWidth: width)
    }

    debugPrint("creating context")
    let context = CGContext(data: nil,
                            width: width,
                            height: totalHeight,
                            bitsPerComponent: 8,
                            bytesPerRow: 0,
                            space: CGColorSpace(name: CGColorSpace.sRGB)!,
                            bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)!

    var top = totalHeight
    for photo in thumbPhotos {
        let height = photo.heightOfImage(ofWidth: width)
        indices.append(top - totalHeight)
        top -= height
        debugPrint("drawing \(photo.filteredFileURL())")
        context.draw(photo.image,
                     in: CGRect(x: 0, y: top, width: width, height: height),
                     byTiling: false)
    }
    debugPrint("write jpeg")
    writeJpegFromContext(context: context, filename: filename)
    return indices
}

func writeJpegFromContext(context: CGContext, filename: URL) {
    let cgImage = context.makeImage()!
    let bitmapRep = NSBitmapImageRep(cgImage: cgImage)
    let jpegData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
    try! jpegData.write(to: filename)
}

sample of output:

"drawing 0002-_MG_8542.jpg"
"drawing 0003-_MG_8545.jpg"
"drawing 0004-_MG_8550.jpg"
IOSurface creation failed: e00002c2 parentID: 00000000 properties: {
    IOSurfaceAddress = 5211357184;
    IOSurfaceAllocSize = 9983331;
    IOSurfaceCacheMode = 0;
    IOSurfaceName = CMPhoto;
    IOSurfacePixelFormat = 1246774599;
}
"drawing 0005-_MG_8555.jpg"
IOSurface creation failed: e00002c2 parentID: 00000000 properties: {
    IOSurfaceAddress = 5221351424;
    IOSurfaceAllocSize = 10041215;
    IOSurfaceCacheMode = 0;
    IOSurfaceName = CMPhoto;
    IOSurfacePixelFormat = 1246774599;
}
"drawing 0006-_MG_8562.jpg"
"drawing 0007-_MG_8563.jpg"
IOSurface creation failed: e00002c2 parentID: 00000000 properties: {
    IOSurfaceAddress = 5376163840;
    IOSurfaceAllocSize = 10109756;
    IOSurfaceCacheMode = 0;
    IOSurfaceName = CMPhoto;
    IOSurfacePixelFormat = 1246774599;
}
"drawing 0008-_MG_8584.jpg"
"drawing 0009-_MG_8618.jpg"
IOSurface creation failed: e00002c2 parentID: 00000000 properties: {
    IOSurfaceAddress = 5394612224;
    IOSurfaceAllocSize = 8425564;
    IOSurfaceCacheMode = 0;
    IOSurfaceName = CMPhoto;
    IOSurfacePixelFormat = 1246774599;
}
"drawing 0010-_MG_8627.jpg"
"drawing 0011-_MG_8649.jpg"
"drawing 0012-_MG_8658.jpg"
"drawing 0013-_MG_8665.jpg"
"drawing 0014-_MG_8677.jpg"
"drawing 0015-_MG_8675.jpg"
"drawing 0016-_MG_8676.jpg"
"drawing 0017-IMGP0873.jpg"
"drawing 0018-_MG_8719.jpg"
"drawing 0019-_MG_8743.jpg"
...

are these jpgs all the same size (width/height)?

The source images are different sizes.

I'm drawing them to a CGRect that scales them down to a common width and a height that maintains the aspect ration of the image I'm drawing in.

(For context, I'm basically creating a web sprite of very low-resolution copies of the input images all stacked on top of each other. I'm getting the result I want, just would like to resolve this error)

I'm seeing the same issue. Frustrating not to be able to find any info...

Similar case - generating thumbnails, though in this case for ML analysis

"IOSurface creation failed" drawing to CGContext
 
 
Q