CGContext draw() works but logs "IOSurface creation failed"

82 Views Asked by At

I'm using the following code to create a single jpeg image that contains significantly scaled down images for an array of images passed in (stacked on top of each other vertically)

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)
}

The code runs fine and produces the desired image. However, my console displays "IOSurface creation failed:" messages from some, but not all, of the context.draw() calls.

I'd prefer a nice clean console (devoid of error messages at runtime), but also curious as to whether there's a bigger issue to be concerned about.

Is there something I can do to avoid this error?

Sample output from a call...

"creating context"
"drawing 0001-_MG_8536.jpg"
"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"
...
0

There are 0 best solutions below